Skip to content

Commit

Permalink
feat: make global variables behave like local variables
Browse files Browse the repository at this point in the history
  • Loading branch information
nawetimebomb committed Oct 9, 2024
1 parent ec4523a commit 857749e
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 40 deletions.
7 changes: 1 addition & 6 deletions compiler/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,7 @@ func generateLinuxX64() {
offset := value.(int)
out.WriteText(" mov rax, program_static_mem")
out.WriteText(" add rax, %d", offset)
out.WriteText(" push rax")
// case OP_PUSH_VAR_GLOBAL:
// offset := value.(int)
// out.WriteText(" mov rax, program_static_mem")
// out.WriteText(" add rax, %d", offset)
// out.WriteText(" push QWORD [rax]")
out.WriteText(" push QWORD [rax]")
case OP_PUSH_VAR_GLOBAL_ADDR:
offset := value.(int)
out.WriteText(" mov rax, program_static_mem")
Expand Down
1 change: 1 addition & 0 deletions compiler/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Frontend struct {
// only be populated once, so if the user tries to generate a new
// body before flushing this stack, it should get an error.
bodyStack []Token
constants []Constant
globals []Object
current *Function
error bool
Expand Down
8 changes: 7 additions & 1 deletion compiler/ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const (
OP_PUSH_VAR_GLOBAL = "OP_PUSH_VAR_GLOBAL"
OP_PUSH_VAR_GLOBAL_ADDR = "OP_PUSH_VAR_GLOBAL_ADDR"
OP_PUSH_VAR_LOCAL = "OP_PUSH_VAR_LOCAL"
OP_PUSH_VAR_LOCAL_ADDR = "OP_PUSH_VAR_LOCAL_ADDR"
OP_PUSH_VAR_LOCAL_ADDR = "OP_PUSH_VAR_LOCAL_ADDR"

// FLOW CONTROL
OP_IF_START = "OP_IF_START"
Expand Down Expand Up @@ -87,6 +87,11 @@ type Arity struct {
types []Argument
}

type Constant struct {
word string
value int
}

type Bind struct {
id int
name string
Expand Down Expand Up @@ -130,6 +135,7 @@ type Function struct {
bindings Binding
code []Code
scope []Scope
constants []Constant
variables []Variable

localMemorySize int
Expand Down
15 changes: 9 additions & 6 deletions compiler/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (

const STACK_SIZE = 1024

type TypeInfo struct {

}

type Typecheck struct {
stack [STACK_SIZE]DataType
stackCount int
Expand Down Expand Up @@ -221,12 +225,11 @@ func ValidateRun() {
case OP_PUSH_STR:
tc.push(DATA_STR)
case OP_PUSH_VAR_GLOBAL:
tc.push(DATA_PTR)
// for _, v := range TheProgram.variables {
// if v.offset == value.(int) {
// tc.push(v.dtype)
// }
// }
for _, v := range TheProgram.variables {
if v.offset == value.(int) {
tc.push(v.dtype)
}
}
case OP_PUSH_VAR_GLOBAL_ADDR:
tc.push(DATA_PTR)
case OP_PUSH_VAR_LOCAL:
Expand Down
20 changes: 20 additions & 0 deletions tests/memory.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using io
using strings

var global int

fn main ()
var local int

"Save values to local and global memory and print them" println
"Also, using them in arithmetics" println
"Global is 13, local is 17" println
13 &global !
17 &local !

"Global: " print global println
"Local: " print local println

"Result should be 30" println
"Result: " print global local + println
ret
25 changes: 0 additions & 25 deletions tests/memory.sk.fix

This file was deleted.

9 changes: 7 additions & 2 deletions tests/memory.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
Save the number to static memory and print it
The number is 1234567890123 1234567890123
Save values to local and global memory and print them
Also, using them in arithmetics
Global is 13, local is 17
Global: 13
Local: 17
Result should be 30
Result: 30

0 comments on commit 857749e

Please sign in to comment.