commit 350c639d23dfa9865a4bb5023fae6b32d8a34213
parent ad7b5d853250e600a291b35eab85c2e20af508e3
Author: Brian Swetland <swetland@frotz.net>
Date: Fri, 20 Oct 2023 19:34:54 -0700
compiler1: support multiple source files
Diffstat:
5 files changed, 44 insertions(+), 15 deletions(-)
diff --git a/Makefile b/Makefile
@@ -29,6 +29,9 @@ out/%.impl.c out/%.type.h out/%.decl.h: %.spl ./out/compiler0
out/%.bin: out/%.impl.c out/%.type.h out/%.decl.h
gcc -g -O0 -Wall -I. -Ibootstrap/inc -Iout -o $@ $<
+out/compiler2: out/compiler1 $(COMPILER_SRC)
+ out/compiler1 $(COMPILER_SRC)
+
clean::
rm -rf bin out
diff --git a/compiler/lexer.spl b/compiler/lexer.spl
@@ -81,7 +81,7 @@ fn unhex(ch u32) i32 {
fn scan() Token {
ctx.byteoffset++;
- var ch i32 = readc(0);
+ var ch i32 = readc(ctx.fd_in);
if ch < 0 {
ctx.cc = 0;
} else {
@@ -291,7 +291,7 @@ fn _next() Token {
} else if tok == tSPC {
continue;
} else if (tok == tMSC) || (tok == tINV) {
- error("unknown character 0x%02x", cc);
+ error("unknown character ", @u32 cc);
}
// if we're an AddOp or MulOp, followed by an '='
diff --git a/compiler/main.spl b/compiler/main.spl
@@ -63,13 +63,33 @@ fn dump_ast(node Ast) {
fn start() i32 {
ctx_init();
- scan();
+ parse_init();
+ ctx.filename = os_arg(0);
- next();
- var ast Ast = parse_program();
+ var n u32 = 1;
+ while n < os_arg_count() {
+ var arg str = os_arg(n);
+ if arg[0] == '-' {
+ error("unsupported option '", arg, "'");
+ }
+ ctx.fd_in = fd_open(arg);
+ if ctx.fd_in == -1 {
+ error("cannot open '", arg, "'");
+ }
+ ctx.linenumber = 1;
+ ctx.filename = arg;
- dump_ast(ast);
-
+ scan();
+ next();
+ parse_program();
+
+ fd_close(ctx.fd_in);
+ ctx.fd_in = -1;
+
+ n++;
+ }
+
+ dump_ast(ctx.program);
return 0;
}
diff --git a/compiler/parser.spl b/compiler/parser.spl
@@ -615,10 +615,12 @@ fn parse_enum_def() {
require(tSEMI);
}
-fn parse_program() Ast {
- var program Ast = ast_make_simple(AST_PROGRAM, 0);
- var last Ast = nil;
+fn parse_init() {
+ ctx.program = ast_make_simple(AST_PROGRAM, 0);
+ ctx.last = nil;
+}
+fn parse_program() Ast {
while true {
if ctx.tok == tENUM {
next();
@@ -631,12 +633,12 @@ fn parse_program() Ast {
} else if ctx.tok == tFN {
next();
var node Ast = parse_fn();
- if last == nil {
- program.left = node;
+ if ctx.last == nil {
+ ctx.program.left = node;
} else {
- last.next = node;
+ ctx.last.next = node;
}
- last = node;
+ ctx.last = node;
} else if ctx.tok == tVAR {
next();
parse_var();
@@ -646,6 +648,6 @@ fn parse_program() Ast {
expected("function, variable, or type definition");
}
}
- return program;
+ return ctx.program;
}
diff --git a/compiler/types.spl b/compiler/types.spl
@@ -224,6 +224,7 @@ struct Context {
filename str, // filename of active source
outname str, // base name for output files
+ fd_in i32, // current input file
linenumber u32, // line number of most recent line
lineoffset u32, // position of start of most recent line
byteoffset u32, // position of the most recent character
@@ -242,6 +243,9 @@ struct Context {
global *Scope, // the global scope
cur_fn *Symbol, // fn being parsed
+ program *Ast,
+ last *Ast,
+
idn_if *String, // identifier strings
idn_fn *String,
idn_for *String,