diff --git a/build.zig b/build.zig index bb3f4fa..af7ed4a 100644 --- a/build.zig +++ b/build.zig @@ -1,38 +1,38 @@ const std = @import("std"); pub fn build(b: *std.Build) void { - const target = b.standardTargetOptions(.{}); - const optimize = b.standardOptimizeOption(.{}); + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); - const root_module = b.createModule(.{ - .target = target, - .optimize = optimize, - .link_libc = true, // TODO: no more libc - }); + const root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + .link_libc = true, // TODO: no more libc + }); - const c = b.addExecutable(.{ - .name = "noom", - .root_module = root_module, - }); + const c = b.addExecutable(.{ + .name = "noom", + .root_module = root_module, + }); - c.root_module.addCSourceFiles(.{ - .files = &[_][]const u8{ - "src/helper.c", - "src/lexer.c", - "src/parser.c", - "src/main.c", - } - }); + c.root_module.addCSourceFiles(.{ + .files = &[_][]const u8{ + "src/helper.c", + "src/lexer.c", + "src/parser.c", + "src/main.c", + } + }); - b.installArtifact(c); + b.installArtifact(c); - const run_cmd = b.addRunArtifact(c); - run_cmd.step.dependOn(b.getInstallStep()); + const run_cmd = b.addRunArtifact(c); + run_cmd.step.dependOn(b.getInstallStep()); - if (b.args) |args| { - run_cmd.addArgs(args); - } + if (b.args) |args| { + run_cmd.addArgs(args); + } - const run = b.step("run", "Build and run the project"); - run.dependOn(&run_cmd.step); + const run = b.step("run", "Build and run the project"); + run.dependOn(&run_cmd.step); } diff --git a/src/lexer.c b/src/lexer.c index 979e70f..2530ab7 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -106,6 +106,28 @@ int noomL_iskeyword(const char* s, noom_uint_t len) { return 0; } +const char *noomL_formatTokenType(noomL_TokenType token_type) { + switch (token_type) { + case NOOML_TOKEN_EOF: + return "EOF"; + case NOOML_TOKEN_KEYWORD: + return "keyword"; + case NOOML_TOKEN_WHITESPACE: + return "whitespace"; + case NOOML_TOKEN_IDENTIFIER: + return "identifier"; + case NOOML_TOKEN_STRING: + return "string"; + case NOOML_TOKEN_SYMBOL: + return "symbol"; + case NOOML_TOKEN_NUMBER: + return "number"; + default: + return "unknown"; + } + +} + noomL_ErrorType noomL_lex(const char* s, noom_uint_t start, noomL_Token* token) { const char* str = s + start; diff --git a/src/lexer.h b/src/lexer.h index 143be59..54872eb 100644 --- a/src/lexer.h +++ b/src/lexer.h @@ -32,5 +32,7 @@ int noomL_iswhitespace(char c); noom_uint_t noomL_getsymbol(const char* s); noom_uint_t noomL_getnumber(const char* s); +const char *noomL_formatTokenType(noomL_TokenType token_type); + noomL_ErrorType noomL_lex(const char* s, noom_uint_t start, noomL_Token* token); // TODO: add more error data diff --git a/src/main.c b/src/main.c index 92fd55f..7645fa1 100644 --- a/src/main.c +++ b/src/main.c @@ -15,7 +15,7 @@ void print_node(noomP_Node* node, noom_uint_t depth) { printf("{\n"); tab(depth+1); - printf("type: %d\n", node->type); + printf("type: %s\n", noomP_formatNodeType(node->type)); tab(depth+1); printf("location: %lld\n", node->source_offset); @@ -42,7 +42,7 @@ int main(int argc, char** argv) { while (1) { noomL_lex(code, pos, &token); - printf("%d ", token.type); + printf("%s ", noomL_formatTokenType(token.type)); for (noom_uint_t i = 0; i < token.length; i++) putchar((code + token.offset)[i]); putchar('\n'); diff --git a/src/parser.c b/src/parser.c index 6290b6e..05298ee 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1,6 +1,39 @@ #include "parser.h" #include "helper.h" +const char *noomP_formatNodeType(noomP_NodeType node_type) { + switch (node_type) { + case NOOMP_NODE_PROGRAM: + return "program"; + case NOOMP_NODE_VARNAME: + return "varname"; + case NOOMP_NODE_LOCALDECLARATION: + return "local declaration"; + case NOOMP_NODE_IFSTATEMENT: + return "if statement"; + case NOOMP_NODE_WHILELOOP: + return "while loop"; + case NOOMP_NODE_BLOCK: + return "block"; + case NOOMP_NODE_BREAK: + return "break"; + case NOOMP_NODE_VARIABLE: + return "variable"; + case NOOMP_NODE_NUMBERLITERAL: + return "number literal"; + case NOOMP_NODE_BOOLEANLITERAL: + return "boolean literal"; + case NOOMP_NODE_NILLITERAL: + return "nil literal"; + case NOOMP_NODE_UNARYOPERATOR: + return "unary operator"; + case NOOMP_NODE_BINARYOPERATOR: + return "binary operator"; + default: + return "unknown"; + } +} + int noomP_peek(noomP_Parser* parser, noomL_Token* token) { while (1) { int success = noomL_lex(parser->code, parser->lex_offset, token); diff --git a/src/parser.h b/src/parser.h index 2b87ad9..118295a 100644 --- a/src/parser.h +++ b/src/parser.h @@ -19,7 +19,9 @@ typedef enum noomP_NodeType { NOOMP_NODE_NILLITERAL, NOOMP_NODE_UNARYOPERATOR, - NOOMP_NODE_BINARYOPERATOR + NOOMP_NODE_BINARYOPERATOR, + + NOOMP_NODE_NCOUNT, } noomP_NodeType; typedef struct noomP_Node { @@ -42,6 +44,8 @@ typedef struct noomP_Parser { // todo: track location in code with line/column? noomP_Node* last_node; } noomP_Parser; +const char *noomP_formatNodeType(noomP_NodeType node_type); + int noomP_peek(noomP_Parser* parser, noomL_Token* token); void noomP_skip(noomP_Parser* parser, noomL_Token* token);