Skip to content

Commit

Permalink
feat: code simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
nawetimebomb committed Oct 13, 2024
1 parent b9a19e9 commit a7a829e
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 125 deletions.
12 changes: 6 additions & 6 deletions compiler/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,18 @@ func generateLinuxX64() {

if !function.called {
if !function.internal {
msg := fmt.Sprintf(MsgTypecheckWarningNotCalled, function.name)
msg := fmt.Sprintf(MsgTypecheckWarningNotCalled, function.word)
ReportErrorAtLocation(msg, function.loc)
}
continue
}

if function.name == "main" {
if function.word == "main" {
mainFuncIP = function.ip
}

out.WriteText("fn%d:", function.ip)
out.WriteText(";; start fn %s (%s:%d:%d)", function.name,
out.WriteText(";; start fn %s (%s:%d:%d)", function.word,
function.loc.f, function.loc.l, function.loc.c)
out.WriteText(" sub rsp, %d", function.localMemorySize)
out.WriteText(" mov [return_stack_rsp], rsp")
Expand Down Expand Up @@ -276,10 +276,10 @@ func generateLinuxX64() {
out.WriteText(" %s", s)
}
case OP_FUNCTION_CALL:
fnCall := value.(FunctionCall)
ip := value.(int)
out.WriteText(" mov rax, rsp")
out.WriteText(" mov rsp, [return_stack_rsp]")
out.WriteText(" call fn%d", fnCall.ip)
out.WriteText(" call fn%d", ip)
out.WriteText(" mov [return_stack_rsp], rsp")
out.WriteText(" mov rsp, rax")
case OP_REBIND:
Expand Down Expand Up @@ -327,7 +327,7 @@ func generateLinuxX64() {
}
}

out.WriteText(";; end fn %s (%s:%d:%d)", function.name,
out.WriteText(";; end fn %s (%s:%d:%d)", function.word,
function.loc.f, function.loc.l, function.loc.c)
}

Expand Down
6 changes: 5 additions & 1 deletion compiler/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func ReportErrorAtEOF(msg string) {
}

func ReportErrorAtFunction(fn *Function, err ErrorMessage, args ...any) {
pos := fmt.Sprintf("%s:%d:%d (%s)", fn.loc.f, fn.loc.l, fn.loc.c, fn.name)
pos := fmt.Sprintf("%s:%d:%d (%s)", fn.loc.f, fn.loc.l, fn.loc.c, fn.word)
msg := fmt.Sprintf(string(err), args...)
fmt.Fprintf(os.Stderr, "%s %s\n", pos, msg)
}
Expand All @@ -60,5 +60,9 @@ func ReportErrorAtLocation(err string, loc Location, args ...any) {
}

func ExitWithError(error ErrorCode) {
for _, e := range TheProgram.errors {
ReportErrorAtLocation(e.err, e.token.loc)
}

os.Exit(int(error))
}
Loading

0 comments on commit a7a829e

Please sign in to comment.