From 671638e20609331862a2893f12254b461f6b774c Mon Sep 17 00:00:00 2001 From: Robert Lowry Date: Wed, 20 Nov 2024 20:20:23 -0600 Subject: [PATCH] add some simple constants (#83) * add some simple constants * add MINDISTANCE --- cmd/compile_test/main.go | 2 +- compile.go | 11 +++++++++++ parser.go | 10 ++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/cmd/compile_test/main.go b/cmd/compile_test/main.go index 72003b9..ed6af60 100644 --- a/cmd/compile_test/main.go +++ b/cmd/compile_test/main.go @@ -100,7 +100,7 @@ func main() { compileSuccess++ if len(in.Code) != len(expected.Code) { - fmt.Printf("%s: length mismatch: %d != %d", inPath, len(in.Code), len(expected.Code)) + fmt.Printf("%s: length mismatch: %d != %d\n", inPath, len(in.Code), len(expected.Code)) mismatches++ continue } diff --git a/compile.go b/compile.go index 0e3bb83..9cd03eb 100644 --- a/compile.go +++ b/compile.go @@ -31,12 +31,22 @@ func newCompiler(src []sourceLine, metadata WarriorData, config SimulatorConfig) }, nil } +func (c *compiler) loadConstants() { + c.values["CORESIZE"] = []token{{tokNumber, fmt.Sprintf("%d", c.config.CoreSize)}} + c.values["MAXLENGTH"] = []token{{tokNumber, fmt.Sprintf("%d", c.config.Length)}} + c.values["MAXPROCESSES"] = []token{{tokNumber, fmt.Sprintf("%d", c.config.Processes)}} + c.values["MINDISTANCE"] = []token{{tokNumber, fmt.Sprintf("%d", c.config.Distance)}} + // c.values["CURLINE"] = []token{{tokNumber, "0"}} +} + // load symbol []token values into value map and code line numbers of // instruction labels into the label map func (c *compiler) loadSymbols() { c.values = make(map[string][]token) c.labels = make(map[string]int) + c.loadConstants() + curPseudoLine := 0 for _, line := range c.lines { if line.typ == linePseudoOp { @@ -372,6 +382,7 @@ func (c *compiler) compile() (WarriorData, error) { return WarriorData{}, fmt.Errorf("line %d: %s", line.line, err) } code = append(code, instruction) + // c.values["CURLINE"] = []token{{tokNumber, fmt.Sprintf("%d", len(code))}} } startExpr, err := c.expandExpression(c.startExpr, 0) diff --git a/parser.go b/parser.go index 64f9a0c..a060b23 100644 --- a/parser.go +++ b/parser.go @@ -79,11 +79,21 @@ func newParser(lex *lexer) *parser { line: 1, } p.next() + + p.loadPredefinedSymbols() return p } type parseStateFn func(p *parser) parseStateFn +func (p *parser) loadPredefinedSymbols() { + p.symbols["CORESIZE"] = -1 + p.symbols["MAXLENGTH"] = -1 + p.symbols["MAXPROCESSES"] = -1 + p.symbols["MINDISTANCE"] = -1 + // p.symbols["CURLINE"] = -1 +} + // parse runs the state machine. the main flows are: // // code lines: