lexer + parser: add formatting for enums + build script: spaces -> tabs

This commit is contained in:
2026-04-17 20:20:31 +02:00
parent f09a36fb27
commit 055f56fcc2
6 changed files with 91 additions and 30 deletions

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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

View File

@@ -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');

View File

@@ -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);

View File

@@ -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);