Skip to content

Commit

Permalink
add some simple constants (#83)
Browse files Browse the repository at this point in the history
* add some simple constants

* add MINDISTANCE
  • Loading branch information
bobertlo authored Nov 21, 2024
1 parent 91b1a4e commit 671638e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/compile_test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
11 changes: 11 additions & 0 deletions compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 671638e

Please sign in to comment.