feat: replace AST-based parser with single-pass bytecode compiler and VM

Replace the recursive-descent parser that built an AST (Node* tree) with a single-pass compiler that emits bytecode directly into a Block structure. Remove the parser.c/parser.h files and their AST node types (NodeLiteral, NodeCall, NodeBinaryOp, etc.), add new compiler.c/compiler.h with a Compiler struct and Pratt-style precedence parsing that outputs CODE_CONSTANT and CODE_END instructions, and introduce vm.c/vm.h containing a Fiber-based interpreter with a stack, interpret() loop, and printValue() for OBJ_INT. Update main.c to call compile() then interpret() instead of parse(), remove TOKEN_INDENT/TOKEN_OUTDENT from token.h and dumpTokens(), and adjust the Xcode project to include the new source files while excluding the old parser.
This commit is contained in:
Bob Nystrom
2013-10-22 19:16:39 +00:00
parent 8d29decb14
commit 9bb2ca308e
10 changed files with 512 additions and 457 deletions
+16 -6
View File
@@ -9,8 +9,9 @@
/* Begin PBXBuildFile section */
29AB1F281816E49C004B501E /* lexer.c in Sources */ = {isa = PBXBuildFile; fileRef = 29AB1F1E1816E49C004B501E /* lexer.c */; };
29AB1F291816E49C004B501E /* main.c in Sources */ = {isa = PBXBuildFile; fileRef = 29AB1F201816E49C004B501E /* main.c */; };
29AB1F2A1816E49C004B501E /* parser.c in Sources */ = {isa = PBXBuildFile; fileRef = 29AB1F211816E49C004B501E /* parser.c */; };
29AB1F2C1816E49C004B501E /* token.c in Sources */ = {isa = PBXBuildFile; fileRef = 29AB1F251816E49C004B501E /* token.c */; };
29AB1F2F1816FA66004B501E /* vm.c in Sources */ = {isa = PBXBuildFile; fileRef = 29AB1F2E1816FA66004B501E /* vm.c */; };
29AB1F3218170104004B501E /* compiler.c in Sources */ = {isa = PBXBuildFile; fileRef = 29AB1F3018170104004B501E /* compiler.c */; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
@@ -30,11 +31,13 @@
29AB1F1E1816E49C004B501E /* lexer.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = lexer.c; path = src/lexer.c; sourceTree = SOURCE_ROOT; };
29AB1F1F1816E49C004B501E /* lexer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = lexer.h; path = src/lexer.h; sourceTree = SOURCE_ROOT; };
29AB1F201816E49C004B501E /* main.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = main.c; path = src/main.c; sourceTree = SOURCE_ROOT; };
29AB1F211816E49C004B501E /* parser.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = parser.c; path = src/parser.c; sourceTree = SOURCE_ROOT; };
29AB1F221816E49C004B501E /* parser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = parser.h; path = src/parser.h; sourceTree = SOURCE_ROOT; };
29AB1F251816E49C004B501E /* token.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = token.c; path = src/token.c; sourceTree = SOURCE_ROOT; };
29AB1F261816E49C004B501E /* token.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = token.h; path = src/token.h; sourceTree = SOURCE_ROOT; };
29AB1F271816E49C004B501E /* wren.1 */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.man; name = wren.1; path = src/wren.1; sourceTree = SOURCE_ROOT; };
29AB1F2D1816FA5B004B501E /* vm.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = vm.h; path = src/vm.h; sourceTree = SOURCE_ROOT; };
29AB1F2E1816FA66004B501E /* vm.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = vm.c; path = src/vm.c; sourceTree = SOURCE_ROOT; };
29AB1F3018170104004B501E /* compiler.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = compiler.c; path = src/compiler.c; sourceTree = SOURCE_ROOT; };
29AB1F3118170104004B501E /* compiler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = compiler.h; path = src/compiler.h; sourceTree = SOURCE_ROOT; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -67,13 +70,15 @@
29AB1F081816E3AD004B501E /* src */ = {
isa = PBXGroup;
children = (
29AB1F3018170104004B501E /* compiler.c */,
29AB1F3118170104004B501E /* compiler.h */,
29AB1F1E1816E49C004B501E /* lexer.c */,
29AB1F1F1816E49C004B501E /* lexer.h */,
29AB1F201816E49C004B501E /* main.c */,
29AB1F211816E49C004B501E /* parser.c */,
29AB1F221816E49C004B501E /* parser.h */,
29AB1F251816E49C004B501E /* token.c */,
29AB1F261816E49C004B501E /* token.h */,
29AB1F2E1816FA66004B501E /* vm.c */,
29AB1F2D1816FA5B004B501E /* vm.h */,
29AB1F271816E49C004B501E /* wren.1 */,
);
name = src;
@@ -132,7 +137,8 @@
buildActionMask = 2147483647;
files = (
29AB1F291816E49C004B501E /* main.c in Sources */,
29AB1F2A1816E49C004B501E /* parser.c in Sources */,
29AB1F3218170104004B501E /* compiler.c in Sources */,
29AB1F2F1816FA66004B501E /* vm.c in Sources */,
29AB1F2C1816E49C004B501E /* token.c in Sources */,
29AB1F281816E49C004B501E /* lexer.c in Sources */,
);
@@ -212,6 +218,8 @@
29AB1F101816E3AD004B501E /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
GCC_C_LANGUAGE_STANDARD = c89;
GCC_WARN_PEDANTIC = NO;
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
@@ -219,6 +227,8 @@
29AB1F111816E3AD004B501E /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
GCC_C_LANGUAGE_STANDARD = c89;
GCC_WARN_PEDANTIC = NO;
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;