Skip to content

Commit

Permalink
feat: add namespacing for foreign and unit tests for C interoperability
Browse files Browse the repository at this point in the history
  • Loading branch information
nawetimebomb committed Oct 16, 2024
1 parent 28b4064 commit 386ed4b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
15 changes: 10 additions & 5 deletions compiler/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func generateLinuxX64() {
out.WriteMetadata("public main")
for _, f := range TheProgram.chunks {
if f.foreign {
out.WriteMetadata("extrn %s", f.word)
out.WriteMetadata("extrn %s", f.name)
}
}
out.WriteCode("section '.text' executable")
Expand Down Expand Up @@ -307,7 +307,7 @@ func generateLinuxX64() {
out.WriteCode(" pop %s", registers[i])
}

out.WriteCode(" call %s", function.word)
out.WriteCode(" call %s", function.name)

if hasResults {
out.WriteCode(" push rax")
Expand Down Expand Up @@ -367,9 +367,14 @@ func generateLinuxX64() {
out.WriteCode(" mov rax, return_stack_rsp_end")
out.WriteCode(" mov [return_stack_rsp], rax")
out.WriteCode(" call fn%d", mainFuncIP)
out.WriteCode(" mov rax, 60")
out.WriteCode(" mov rdi, 0")
out.WriteCode(" syscall")
if TheProgram.libcEnabled {
out.WriteCode(" mov rdi, 0")
out.WriteCode(" call exit")
} else {
out.WriteCode(" mov rax, 60")
out.WriteCode(" mov rdi, 0")
out.WriteCode(" syscall")
}
}

func CodegenRun(out *OutputCode) {
Expand Down
14 changes: 13 additions & 1 deletion compiler/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const (
type Function struct {
ip int
loc Location
name string
namespace string
token Token
word string

Expand Down Expand Up @@ -373,6 +375,14 @@ func createConstant() {
func createForeigns() {
t := parser.previousToken

if !match(TOKEN_WORD) {
t := parser.previousToken
ReportErrorAtLocation(DeclarationWordMissing, t.loc)
ExitWithError(ParseError)
}

namespace := parser.previousToken.value.(string)

consume(TOKEN_CURLY_BRACKET_OPEN,
UnexpectedSymbol, parser.previousToken.kind, "{")

Expand All @@ -392,7 +402,9 @@ func createForeigns() {
advance()

tokenWord := parser.previousToken
function.word = tokenWord.value.(string)
function.namespace = namespace
function.name = tokenWord.value.(string)
function.word = function.namespace + "." + function.name
consume(TOKEN_PAREN_OPEN, UnexpectedSymbol, t.kind, "(")
parsingArguments := true

Expand Down
8 changes: 5 additions & 3 deletions libs/libc.sk
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
; all the C required procedures, making compilation slower and final file
; size bigger. 'libc' is linked statically.

foreign {
fn printf (any str) ...
fn puts (str) ...
foreign c {
fn exit (int) ...
fn fprintf (int any str) ...
fn printf (any str) ...
fn puts (str) ...
}
6 changes: 6 additions & 0 deletions tests/c-printf.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using libc
using unix

fn main ()
"Stańczyk" "Hello, %s\n" c.printf
ret
1 change: 1 addition & 0 deletions tests/c-printf.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, Stańczyk

0 comments on commit 386ed4b

Please sign in to comment.