Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add some simple constants #83

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading