Tải bản đầy đủ

Game scripting mastery

TE
AM
FL
Y


Game
Scripting
Mastery
Alex Varanese


© 2003 by Premier Press, a division of Course Technology. All rights reserved. No part of this
book may be reproduced or transmitted in any form or by any means, electronic or mechanical,
including photocopying, recording, or by any information storage or retrieval system without
written permission from Premier Press, except for the inclusion of brief quotations in a review.
The Premier Press logo and related trade dress are trademarks of Premier Press, Inc. and
may not be used without written permission.
Publisher: Stacy L. Hiquet
Marketing Manager: Heather Hurley
Acquisitions Editor: Mitzi Koontz

Series Editor: André LaMothe
Project Editor: Estelle Manticas
Copy Editor: Kezia Endsley
Interior Layout: Bill Hartman
Cover Designer: Mike Tanamachi
Indexer: Kelly Talbot
Proofreader: Sara Gullion
ActivePython, ActiveTcl, and ActiveState are registered trademarks of the ActiveState
Corporation. All other trademarks are the property of their respective owners.
Important: Premier Press cannot provide software support. Please contact the appropriate
software manufacturer’s technical support line or Web site for assistance.
Premier Press and the author have attempted throughout this book to distinguish proprietary
trademarks from descriptive terms by following the capitalization style used by the manufacturer.
Information contained in this book has been obtained by Premier Press from sources believed to
be reliable. However, because of the possibility of human or mechanical error by our sources,
Premier Press, or others, the Publisher does not guarantee the accuracy, adequacy, or
completeness of any information and is not responsible for any errors or omissions or the results
obtained from use of such information. Readers should be particularly aware of the fact that the
Internet is an ever-changing entity. Some facts may have changed since this book went to press.
ISBN: 1-931841-57-8
Library of Congress Catalog Card Number: 2001099849
Printed in the United States of America
03 04 05 06 07 BH 10 9 8 7 6 5 4 3 2 1
Premier Press, a division of Course Technology
2645 Erie Avenue, Suite 41
Cincinnati, Ohio 45208


This book is dedicated to my parents, Ray and Sue, and to my
sister Katherine, if for no other reason than the simple fact that
they'd put me in a body bag if I forgot to do so.


iv

Foreword
Programming games is so fun! The simple reason is that you get to code so many different types
of subsystems in a game, regardless of whether it's a simple Pac Man clone or a complex triple-A
tactical shooter. Coding experience is very enriching, whether you’re writing a renderer, sound
system, AI system, or the game code itself; all of these types of programming contain challenges


that you get to solve. The best way to code in any of these areas is with the most knowledge you
can absorb beforehand. This is why you should have a ton of programming books close at hand.
One area of game coding that hasn't gotten much exposure is scripting. Some games don't need
scripting—whether or not a game does is often dependant on your development environment
and team—but in a lot of cases, using scripting is an ideal way of isolating game code from the
main engine, or even handling in-game cinematics. Most programmers, when faced with solving
a particular coding problem (let's say handling NPC interaction, for instance), will usually decide
to write their own elaborate custom language that integrates with their game code. With the
scripting tools available today this isn't strictly necessary, but boy is it fun!
Many coders aren’t aware of the range of scripting solutions available today; that’s where this fine
book comes in. Game Scripting Mastery is the best way to dive into the mysterious world of game
scripting languages. You’ll learn what a scripting language is and how one is written; you’ll get to
learn about Lua, Python, and Tcl and how to make them work with your game (I’m a hardcore
proponent for Lua, by the way); and, of course, you’ll learn about compiler theory. You’ll even
get to examine how a full scripting language is developed! There's lots of knowledge contain
herein, and if you love coding games, I'm confident that you'll enjoy finding out more about this
aspect of game programming. Have "The Fun!”

John Romero


v

Acknowledgments
It all started as I was standing around with some friends of mine on the second day of the 2001
Xtreme Game Developer's Conference in Santa Clara, California, discussing the Premier Press
game development series. At the time, I'd been doing a lot of research on the subject of compiler
theory—specifically, how it could be applied to game scripting—and at the exact moment I mentioned that a scripting book would be a good idea, André Lamothe just happened to walk by.
"Let's see what he thinks," I said, and pulled him aside. "Hey André, have you ever thought about
a book on game scripting for your series?" I expected something along the lines of "that's not a
bad idea", or "sure-- it's already in production." What I got was surprising, to say the least.
"Why don't you write it?"
That was literally what he said. Unless you're on some sort of weird version of Jeopardy! where the
rules of the game require you to phrase your answer in the form of a book deal, this is a pretty
startling response. I blinked, thought about it for about a nanosecond, and immediately said
okay. This is how I handle most important decisions, but the sheer magnitude of the events that
would be set into motion by this particular one could hardly have been predicted at the time.
Never question the existence of fate.
With the obligatory anecdote out of the way, there are a number of very important people I'd like
to thank for providing invaluable support during the production of this book. It'd be nothing
short of criminal if this list didn't start with Mitzi Foster, my acquisitions editor who demonstrated
what can only be described as superhuman patience during the turbulent submission and evolution of the book's manuscript. Having to handle the eleventh-hour rewrites of entire chapters
(and large ones at that) after they've been submitted and processed is an editor's nightmare—
and only one of the many she put up with—but she managed to handle it in stride, with a consistently friendly and supportive attitude.
Next up is my copy editor, Kezia Endsley; if you notice the thorough grammatical correctness of
even the comments in this book's code listings, you'll have her to thank. Granted, it'll only be a
matter of time before the latest version of Microsoft's compilers have a comment grammar checking paperclip, dancing monkey, robot dog, or ethnically ambiguous baby, but her eye for detail is
safely appreciated for now.
Lastly, rounding out the Game Scripting Mastery pit crew is Estelle Manticas, my project editor
who really stepped up to the plate during the later parts of the project, somehow maintaining a
sense of humor while planet Earth crumbled around us. Few people have what it takes to manage
the workload of an entire book when the pressure's on, and she managed to make it look easy.


vi

Of course, due to my relatively young age and penchant for burning through cash like NASA, I've
relied on others to provide a roof over my head. The honor here, not surprisingly, goes to my
parents. I'd like to thank my mom for spreading news of my book deal to every friend, relative,
teacher, and mailman our family has ever known, and my dad for deciding that the best time to
work so loudly on rebuilding the deck directly outside my room is somewhere around zero o'clock
in the morning. I also can't forget my sister, Katherine—her constant need for me to drive her to
work is the only thing that keeps me waking up at a decent hour. Thanks a lot, guys!
And last, and most certainly least, I suppose I should thank that Lamothe guy. Seriously though—I
may have toiled endlessly on the code and manuscript, but André is the real reason this book
happened (and was also its technical editor). I've gotta say thanks for letting my raid your fridge
on a regular basis, teaching me everything I know about electrical engineering, dumping so many
free books on me, answering my incessant and apparently endless questions, restraining yourself
from ending our more heated arguments with a golf club, and of course, extending such an
obscenely generous offer to begin with. It should be known that there's literally no one else in
the industry that goes out of their way to help people out this much, and I'm only one of many
who've benefited from it.
I'd also like to give a big thanks to John Romero, who took time out of his understandably
packed schedule to save the day and write the book's Foreword. If not for him, I probably
would've had to get my mom to do it.
Oh and by the way, just because I think they'll get a kick out of it, I'd like to close with some horrendously geeky shout-outs: thanks to Ironblayde, xms and Protoman—three talented coders,
and the few people I actually talk to regularly online—for listening to my constant ranting, and
encouraging me to finish what I start (if for no other reason than the fact that I'll stop blabbering
about it). You guys suck. Seriously.
Now if you'll excuse me, I'm gonna wrap this up. I feel like I'm signing a yearbook.


vii

About the Author
Alex Varanese has been obsessed with game development since the mid-1980's when, at age five,
he first laid eyes—with both fascination and a strange and unexplainable sense of familiarity—on
the 8-bit Nintendo Entertainment System. He's been an avid artist since birth as well, but didn't
really get going as a serious coder until later in life, at around age 15, with QBASIC. He got his
start as a professional programmer at age 18 as a Java programmer in the Silicon Valley area,
working on a number of upstart B2B projects on the J2EE platform before working for about a
year as both a semi-freelance and in-house graphic designer.
Feeling that life in the office was too restrictive, however, he's since shifted his focus back to game
development and the pursuit of future technology. He currently holds the position of head
designer and systems architect for eGameZone (http://www.egamezone.net), the successor venture
to André LaMothe's Xtreme Games LLC. He spends his free time programming, rendering, writing about himself in the third person, yelling at popup ads, starring in an off-Broadway production of Dude, Where's My Car? The Musical, and demonstrating a blatant disregard for the posted
speed limit.
Alex Varanese can be reached at alex@amvbooks.com, and is always ready and willing to answer any
questions you may have about the book. Please, don't hesitate to ask!


viii

Letter from the
Series Editor
A long, long, time ago on an 8-bit computer far, far, away, you could get
away with hard coding all your game logic, artificial intelligence, and so
forth. These days, as they say on the Sopranos "forget about it.…" Games
are simply too complex to even think about coding anymore—in fact, 99
percent of all commercial games work like this: a 3D game engine is developed, then an interface to the engine is created via a scripting language system (usually a very high-level language) based on a virtual machine. The
scripting language is used by the game programmers, and even more so the
game designers, to create the actual game logic and behaviors for the entire
game. Additionally, many of the rules of standard programming, such as
strict typing and single threaded execution, are broken with scripting languages. In essence, the load of game development falls to the game designers for logic and game play, and to game programmers for the 3D engine,
physics, and core technologies of the engine.
So where does one start when learning to use scripting in games? Well,
there's a lot of stuff on the Internet of course, and you can try to interface
languages like Python, Lau, and others to your game, but I say you should
know how to do it yourself from the ground up. And that’s what Game
Scripting Mastery is all about. This book is a monster—Alex covers every
detail you can possibly imagine about game scripting.
This is hard stuff, relatively speaking—we are talking about compiler theory,
virtual machines, and multithreading here. However, Alex starts off assuming you know nothing about scripting or compilers, so even if you’re a
beginner you will be able to easily follow along, provided you take your time
and work through the material. By the end of the book you’ll be able to
write a compiler and a virtual machine, as well as interface your language to


ix

your existing C/C++ game engine—in essence, you will have mastered
game scripting! Also, you will never want to write another parser as long as
you live.
In conclusion, if game scripting is something you’ve been interested in, and
you want to learn it in some serious detail, then this book is the book for
you. Moreover, this is the only book on the market (as we go to publication)
about this subject. As this is the flagship treatise on game scripting, we’ve
tried to give you everything we needed when figuring it out on our own—
and I think we have done much, much more. You be the judge!
Sincerely,

André LaMothe
Series Editor


CONTENTS

AT A

GLANCE

Contents at a Glance

AM
FL
Y

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xliv

Part One
Scripting Fundamentals ..........................1
Chapter 1
An Introduction to Scripting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Chapter 2
Applications of Scripting Systems. . . . . . . . . . . . . . . . . . . . . . . . 29

TE

x

Part Two
Command-Based Scripting ...................61
Chapter 3
Introduction to Command-Based Scripting . . . . . . . . . . . . . . . . 63
Chapter 4
Advanced Command-Based Scripting . . . . . . . . . . . . . . . . . . . 113

Part Three
Introduction to Procedural
Scripting Languages ...........................153
Chapter 5
Introduction to Procedural Scripting Systems . . . . . . . . . . . . . 155

Team-Fly®


CONTENTS

AT A

GLANCE

Chapter 6
Integration: Using Existing Scripting Systems . . . . . . . . . . . . . 173
Chapter 7
Designing a Procedural Scripting Language . . . . . . . . . . . . . . . 335

Part Four
Designing and Implementing a
Low-Level Language ..........................367
Chapter 8
Assembly Language Primer. . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Chapter 9
Building the XASM Assembler . . . . . . . . . . . . . . . . . . . . . . . . . 411

Part Five
Designing and Implementing a
Virtual Machine ..................................565
Chapter 10
Basic VM Design and Implementation . . . . . . . . . . . . . . . . . . . 567
Chapter 11
Advanced VM Concepts and Issues . . . . . . . . . . . . . . . . . . . . . . 651

Part Six
Compiling High-Level Code................749
Chapter 12
Compiler Theory Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . 751
Chapter 13
Lexical Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783
Chapter 14
Building the XtremeScript Compiler Framework . . . . . . . . . . 857

xi


xii

CONTENTS

AT A

GLANCE

Chapter 15
Parsing and Semantic Analysis . . . . . . . . . . . . . . . . . . . . . . . . . 983

Part Seven
Completing Your Training ..................1137
Chapter 16
Applying the System to a Full Game . . . . . . . . . . . . . . . . . . . 1139
Chapter 17
Where to Go From Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1179
Appendix A
What’s on the CD? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1203

INDEX........................................1207


CONTENTS

xiii

Contents
Introduction ..................................xliv

Part One
Scripting Fundamentals...............1
Chapter 1
An Introduction to Scripting................3
What Is Scripting? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Structured Game Content—A Simple Approach. . . . . . . . . . . . . 6
Improving the Method with Logical and Physical Separation . . 10
The Perils of Hardcoding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Storing Functionality in External Files . . . . . . . . . . . . . . . . . . . . 14
How Scripting Actually Works. . . . . . . . . . . . . . . . . . . . . . . . . . . 15
An Overview of Computer Programming . . . . . . . . . . . . . . . . . . . . . . . . . . 16
An Overview of Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

The Fundamental Types of Scripting Systems . . . . . . . . . . . . . . 20
Procedural/Object-Oriented Language Systems . . . . . . . . . . . . . . . . . . . . . . 21
Command-Based Language Systems. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Dynamically Linked Module Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Compiled versus Interpreted Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Existing Scripting Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Lua. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27


xiv

CONTENTS

Chapter 2
Applications of Scripting Systems.......29
The General Purpose of Scripting . . . . . . . . . . . . . . . . . . . . . . . 30
Role Playing Games (RPGs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Complex, In-Depth Stories . . . .
The Solution . . . . . . . . . . . .
Non-Player Characters (NPCs) .
The Solution . . . . . . . . . . . .
Items and Weapons . . . . . . . . . .
The Solution . . . . . . . . . . . .
Enemies . . . . . . . . . . . . . . . . . . .
The Solution . . . . . . . . . . . .

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

32
33
34
35
41
43
45
46

First-Person Shooters (FPSs) . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Objects, Puzzles, and Switches (Obligatory Oh My!)
The Solution . . . . . . . . . . . . . . . . . . . . . . . . . . .
Enemy AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Solution . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

51
52
57
59

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

Part Two
Command-Based Scripting ..........61
Chapter 3
Introduction to Command-Based
Scripting.......................................63
The Basics of Command-Based Scripting. . . . . . . . . . . . . . . . . . 64
High-Level Engine Control. . . . . . .
Commands . . . . . . . . . . . . . . . . . .
Master of Your Domain . . . . . . . . .
Actually Getting Something Done .

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

65
68
68
69

Command-Based Scripting Overview. . . . . . . . . . . . . . . . . . . . . 69
Engine Functionality Assessment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Loading and Executing Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Looping Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73


CONTENTS

xv

Implementing a Command-Based Language . . . . . . . . . . . . . . . 74
Designing the Language . . . . . . . . . . . . . .
Writing the Script . . . . . . . . . . . . . . . . . .
Implementation . . . . . . . . . . . . . . . . . . . .
Basic Interface . . . . . . . . . . . . . . . . . .
Execution. . . . . . . . . . . . . . . . . . . . . .
Command and Parameter Extraction.
The Command Handlers . . . . . . . . . .

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

74
75
75
75
78
81
87

Scripting a Game Intro Sequence. . . . . . . . . . . . . . . . . . . . . . . . 90
The Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
The Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
The Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

Scripting an RPG Character’s Behavior . . . . . . . . . . . . . . . . . . . 95
The Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Improving the Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Managing a Game Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
The Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
The Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
The Demo’s Main Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

Concurrent Script
Summary . . . . . . .
On the CD . . . . . .
Challenges . . . . . .

Execution
.........
.........
.........

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

109
110
111
111

Chapter 4
Advanced Command-Based Scripting.....113
New Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Boolean Constants . . . . . . . . . . . . . .
Floating-Point Support . . . . . . . . . . . .
General-Purpose Symbolic Constants
An Internal Constant List. . . . . . .
A Two-Pass Approach. . . . . . . . . .
Loading Before Executing. . . . . . .

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

115
115
116
117
120
124


xvi

CONTENTS

Simple Iterative and Conditional Logic . . . . . . . . . . . . . . . . . . 125
Conditional Logic and Game Flags .
Grouping Code with Blocks . . . . .
The Block List . . . . . . . . . . . . . . . .
Iterative Logic . . . . . . . . . . . . . . . .
Nesting . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

125
128
129
131
133

Event-Based Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Compiling Scripts to a Binary Format . . . . . . . . . . . . . . . . . . . 137
Increased Execution Speed . . . . .
Detecting Compile-Time Errors .
Malicious Script Hacking . . . . . . .
How a CBL Compiler Works. . . .
Executing Compiled Scripts . .
Compile-Time Preprocessing .
Parameters. . . . . . . . . . . . . . .

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

137
139
139
140
142
143
144

Basic Script Preprocessing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
File-Inclusion Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150

Part Three
Introduction to Procedural
Scripting Languages ...............153
Chapter 5
Introduction to Procedural
Scripting Systems ..........................155
Overall Scripting Architecture . . . . . . . . . . . . . . . . . . . . . . . . . 156
High-Level Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Low-Level Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
The Virtual Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

A Deeper Look at XtremeScript . . . . . . . . . . . . . . . . . . . . . . . 161
High-Level Code/Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Lexical Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164


CONTENTS

Parsing/Syntactic Analysis . . . . . . . . .
Semantic Analysis . . . . . . . . . . . . . . .
Intermediate Code Generation . . . .
Optimization . . . . . . . . . . . . . . . . . .
Assembly Language Generation . . . .
The Symbol Table . . . . . . . . . . . . . . .
The Front End versus the Back End .
Low-Level Code/Assembly. . . . . . . . . . .
The Assembler . . . . . . . . . . . . . . . . .
The Disassembler . . . . . . . . . . . . . .
The Debugger . . . . . . . . . . . . . . . . .
The Virtual Machine . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

xvii

.
.
.
.
.
.
.
.
.
.
.
.

164
165
165
165
166
166
166
167
167
167
167
168

The XtremeScript System . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
High-Level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Low-Level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Runtime. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171

Chapter 6
Integration: Using Existing
Scripting Systems ..........................173
Integration . . . . . . . . . . . . . . . . . . . . . .
Implementation of Scripting Systems.
The Bouncing Head Demo . . . . . . . . .
Lua (and Basic Scripting Concepts) . .
The Lua System at a Glance . . . . .
The Lua Library . . . . . . . . . . . .
The luac Compiler . . . . . . . . . .
The lua Interactive Interpreter.
The Lua Language . . . . . . . . . . . . .
Comments . . . . . . . . . . . . . . . .
Variables . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

174
179
181
185
.
.
.
.
.
.
.

185
185
185
186
187
188
188


xviii

CONTENTS

Data Types . . . . . . . . . . . . . . . . . . . . . . . .
Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . .
Advanced String Features. . . . . . . . . . . . .
Expressions . . . . . . . . . . . . . . . . . . . . . . .
Conditional Logic. . . . . . . . . . . . . . . . . . .
Iteration. . . . . . . . . . . . . . . . . . . . . . . . . .
Functions . . . . . . . . . . . . . . . . . . . . . . . . .
Integrating Lua with C . . . . . . . . . . . . . . . . . .
Compiling a Lua Project. . . . . . . . . . . . . .
Initializing Lua . . . . . . . . . . . . . . . . . . . . .
Loading Scripts. . . . . . . . . . . . . . . . . . . . .
The Lua Stack . . . . . . . . . . . . . . . . . . . . .
Exporting C Functions to Lua . . . . . . . . .
Executing Lua Scripts . . . . . . . . . . . . . . . .
Importing Lua Functions . . . . . . . . . . . . .
Manipulating Global Lua Variables from C
Re-coding the Alien Demo in Lua . . . . . .
Advanced Lua Topics . . . . . . . . . . . . . . . . . . .
Web Links . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

191
193
197
198
200
201
203
205
206
207
208
209
215
219
221
226
228
241
242

Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
The Python System at a Glance . . . . . .
Directory Structure . . . . . . . . . . . .
The Python Interactive Interpreter
The Python Language. . . . . . . . . . . . . .
Comments . . . . . . . . . . . . . . . . . . .
Variables . . . . . . . . . . . . . . . . . . . .
Data Types . . . . . . . . . . . . . . . . . . .
Basic Strings. . . . . . . . . . . . . . . . . .
String Manipulation . . . . . . . . . . . .
Lists . . . . . . . . . . . . . . . . . . . . . . . .
Expressions . . . . . . . . . . . . . . . . . .
Conditional Logic. . . . . . . . . . . . . .
Iteration. . . . . . . . . . . . . . . . . . . . .
Functions . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

242
243
243
244
244
244
246
247
248
251
254
256
258
261


CONTENTS

Integrating Python with C . . . . . . . . .
Compiling a Python Project . . . . .
Initializing Python . . . . . . . . . . . . .
Python Objects . . . . . . . . . . . . . .
Re-coding the Alien Head Demo .
Advanced Topics . . . . . . . . . . . . . . . .
Web Links . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

xix

.
.
.
.
.
.
.

263
263
265
265
277
286
286

Tcl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
ActiveStateTcl . . . . . . . . . . . . . . . . . . . . . . . .
The Distribution at a Glance . . . . . . . . . .
The tclsh Interactive Interpreter . . . . . . .
What, No Compiler? . . . . . . . . . . . . . . . .
Tcl Extensions . . . . . . . . . . . . . . . . . . . . .
The Tcl Language . . . . . . . . . . . . . . . . . . . . . .
Commands—The Basis of Tcl . . . . . . . . .
Substitution . . . . . . . . . . . . . . . . . . . . . . .
Comments . . . . . . . . . . . . . . . . . . . . . . . .
Variables . . . . . . . . . . . . . . . . . . . . . . . . .
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . .
Expressions . . . . . . . . . . . . . . . . . . . . . . .
Conditional Logic. . . . . . . . . . . . . . . . . . .
Iteration. . . . . . . . . . . . . . . . . . . . . . . . . .
Functions (User-Defined Commands) . . .
Integrating Tcl with C . . . . . . . . . . . . . . . . . .
Compiling a Tcl Project . . . . . . . . . . . . . .
Initializing Tcl . . . . . . . . . . . . . . . . . . . . . .
Loading and Running Scripts . . . . . . . . . .
Calling Tcl Commands from C . . . . . . . . .
Exporting C Functions as Tcl Commands.
Returning Values from Tcl Commands . . .
Manipulating Global Tcl Variables from C .
Recoding the Alien Head Demo. . . . . . . .
Advanced Topics . . . . . . . . . . . . . . . . . . . . . .
Web Links . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

288
288
289
290
290
291
291
292
297
298
301
303
306
308
310
312
312
313
314
315
316
319
320
322
330
330


xx

CONTENTS

Which Scripting System Should You Use?
Scripting an Actual Game . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . .
On the CD . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

331
333
333
334

AM
FL
Y

Chapter 7
Designing a Procedural Scripting
Language .....................................335
General Types of Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Assembly-Style Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Upping the Ante . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340

Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344

TE

Object-Oriented Programming . . .
XtremeScript Language Overview .
Design Goals . . . . . . . . . . . . . .
Syntax and Features. . . . . . . . . . . .
Data Structures . . . . . . . . . . . .
Operators and Expressions . . .
Code Blocks . . . . . . . . . . . . . .
Control Structures. . . . . . . . . .
Functions . . . . . . . . . . . . . . . . .
Escape Sequences. . . . . . . . . . .
Comments . . . . . . . . . . . . . . . .
The Preprocessor . . . . . . . . . .
Reserved Word List . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

346
349
349
351
351
354
358
358
361
363
363
363
364

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365

Team-Fly®


CONTENTS

xxi

Part Four
Designing and Implementing
a Low-Level Language .............367
Chapter 8
Assembly Language Primer ..............369
What Is Assembly Language? . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Why Assembly Now? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
How Assembly Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Instructions . . . . . . . . . . . . . . . . . . . .
Operands. . . . . . . . . . . . . . . . . . .
Expressions . . . . . . . . . . . . . . . . .
Jump Instructions . . . . . . . . . . . . .
Conditional Logic. . . . . . . . . . . . .
Iteration. . . . . . . . . . . . . . . . . . . .
Mnemonics versus Opcodes . . . . . . .
RISC versus CISC . . . . . . . . . . . . . . .
Orthogonal Instruction Sets . . . . . . .
Registers . . . . . . . . . . . . . . . . . . . . . .
The Stack . . . . . . . . . . . . . . . . . . . . .
Stack Frames/Activation Records .
Local Variables and Scope. . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

372
372
373
375
377
380
383
386
388
389
389
392
395

Introducing XVM Assembly. . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Initial Evaluations . . . . . . . .
The XVM Instruction Set. .
Memory . . . . . . . . . . . .
Arithmetic . . . . . . . . . .
Bitwise . . . . . . . . . . . . .
String Processing . . . . .
Conditional Branching .
The Stack Interface . . .
The Function Interface.
Miscellaneous. . . . . . . .

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

398
399
399
400
401
402
402
403
403
404


xxii

CONTENTS

XASM Directives . . . .
Stack and Data. . .
Functions . . . . . . .
Escape Sequences.
Comments . . . . . .

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

404
405
406
407
407

Summary of XVM Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409

Chapter 9
Building the XASM Assembler ...........411
How a Simple Assembler Works . . . . . . . . . . . . . . . . . . . . . . . . 413
Assembling Instructions . . . . . . . . . . .
Assembling Variables . . . . . . . . . . . . .
Assembling Operands . . . . . . . . . . . .
Assembling String Literals . . . . . . . . .
Assembling Jumps and Function Calls

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

414
416
420
422
423

XASM Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Memory Management . . . . . . . . . . . . . . . . .
Input: Structure of an XVM Assembly Script
Directives . . . . . . . . . . . . . . . . . . . . . . .
Instructions . . . . . . . . . . . . . . . . . . . . . .
Line Labels . . . . . . . . . . . . . . . . . . . . . . .
Host API Function Calls . . . . . . . . . . . . .
The _Main () Function . . . . . . . . . . . . . .
The _RetVal Register . . . . . . . . . . . . . . .
Comments . . . . . . . . . . . . . . . . . . . . . . .
A Complete Example Script. . . . . . . . . .
Output: Structure of an XVM Executable . .
Overview . . . . . . . . . . . . . . . . . . . . . . . .
The Main Header . . . . . . . . . . . . . . . . . .
The Instruction Stream . . . . . . . . . . . . .
The String Table . . . . . . . . . . . . . . . . . . .
The Function Table. . . . . . . . . . . . . . . . .
The Host API Call Table . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

429
430
431
439
440
440
441
441
442
442
444
444
445
447
451
453
454


CONTENTS

xxiii

Implementing the Assembler . . . . . . . . . . . . . . . . . . . . . . . . . . 455
Basic Lexing/Parsing Theory. . . . . . . . . . . . . . .
Lexing . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Basic String Processing. . . . . . . . . . . . . . . . . . .
Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . .
A String-Processing Library . . . . . . . . . . . .
The Assembler’s Framework . . . . . . . . . . . . . .
The General Interface . . . . . . . . . . . . . . . .
A Structural Overview. . . . . . . . . . . . . . . .
Lexical Analysis/Tokenization . . . . . . . . . . . . . .
The Lexer’s Interface and Implementation .
Error Handling. . . . . . . . . . . . . . . . . . . . . . . . .
Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Initializing the Parser . . . . . . . . . . . . . . . . .
Directives . . . . . . . . . . . . . . . . . . . . . . . . .
Line Labels . . . . . . . . . . . . . . . . . . . . . . . . .
Instructions . . . . . . . . . . . . . . . . . . . . . . . .
Building the .XSE Executable . . . . . . . . . . . . . .
The Header . . . . . . . . . . . . . . . . . . . . . . . .
The Instruction Stream . . . . . . . . . . . . . . .
The String Table . . . . . . . . . . . . . . . . . . . . .
The Function Table. . . . . . . . . . . . . . . . . . .
The Host API Call Table . . . . . . . . . . . . . . .
The Assembly Process . . . . . . . . . . . . . . . . . . .
Loading the Source File . . . . . . . . . . . . . . .
The First Pass . . . . . . . . . . . . . . . . . . . . . .
The Second Pass . . . . . . . . . . . . . . . . . . . .
Producing the .XSE . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

456
457
459
462
462
464
469
470
470
495
496
525
527
528
529
542
543
552
552
553
555
556
557
558
558
559
560
562

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
On the CD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564


xxiv

CONTENTS

Part Five
Designing and Implementing
a Virtual Machine..................565
Chapter 10
Basic VM Design and Implementation ..567
Ghost in the Virtual Machine. . . . . . . . . . . . . . . . . . . . . . . . . . . 568
Mimicking Hardware . . . . . . . . . . . . .
The VM’s Major Components . . . . . .
The Instruction Stream . . . . . . . .
The Runtime Stack. . . . . . . . . . . .
Global Data Tables . . . . . . . . . . . .
Multithreading . . . . . . . . . . . . . . . . . .
Integration with the Host Application

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

569
570
571
571
571
573
573

A Brief Overview of a VM’s Lifecycle . . . . . . . . . . . . . . . . . . . . 574
Loading the Script . . . . . . . . . . . . . . . .
Beginning Execution at the Entry Point
The Execution Cycle . . . . . . . . . . . . . .
Function Calls . . . . . . . . . . . . . . . . . . .
Calling a Function . . . . . . . . . . . . .
Returning From a Function . . . . . .
Termination and Shut Down . . . . . . . .

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

574
576
576
578
578
580
581

Structural Overview of the XVM Prototype. . . . . . . . . . . . . . . 582
The Script Header . . . . . .
Runtime Values. . . . . . . . .
The Instruction Stream . .
The Runtime Stack . . . . .
The Frame Index . . . .
The Function Table. . . . . .
The Host API Call Table . .
The Final Script Structure

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

583
583
584
585
586
587
587
588

Building the XVM Prototype. . . . . . . . . . . . . . . . . . . . . . . . . . . 589
Loading an .XSE Executable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay

×