Skip to content

Commit

Permalink
Added REM statement (comments), some optimizations to variable alloca…
Browse files Browse the repository at this point in the history
…tions, minor output code formatting
  • Loading branch information
maciej-trebacz committed Apr 9, 2012
1 parent 05ec3ee commit 982fad5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
62 changes: 42 additions & 20 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

var data *os.File
var Look byte
var Keywords = []string { "IF", "ELSE", "WHILE", "END", "DIM", "CLS", "PRINT", "LOCATE" }
var Tokens = []byte { 'x', 'i', 'l', 'w', 'e', 'd', 'c', 'p', 'o' }
var Keywords = []string { "IF", "ELSE", "WHILE", "END", "DIM", "CLS", "PRINT", "LOCATE", "REM" }
var Tokens = []byte { 'x', 'i', 'l', 'w', 'e', 'd', 'c', 'p', 'o', 'r' }
var Token byte
var Value string
var LabelCount = 0
Expand Down Expand Up @@ -136,7 +136,7 @@ func AddVar(n string, t rune) {
Symbols[StackDepth].n = n
Symbols[StackDepth].t = t
Symbols[StackDepth].l = -StackDepth
Push()
StackDepth++
}

func IsAlpha(c byte) bool {
Expand Down Expand Up @@ -363,7 +363,9 @@ func BranchFalse(s string) {

func Prolog() {
EmitLine("SET PC, begin")
EmitLine("")
FuncPrint()
EmitLine("")
PostLabel("begin")
}

Expand Down Expand Up @@ -669,41 +671,52 @@ func Loc() {
}

func FuncPrint() {
PostLabel("printnum")
Push()
PostLabel("printchar")
EmitLine("SET B, X") // Get current cursor position
EmitLine("ADD B, 0x8000") // Add video mem address
EmitLine("SET [B], A") // Set video memory byte to show char
EmitLine("ADD X, 1") // Increment cursor position
EmitLine("IFN X, 0x160") // Check if we should do next line (X > 32)
EmitLine("SET PC, pnline")
EmitLine("SET X, 0") // First row, first column
PostLabel("pnline")
Ret()

PostLabel("printint")
EmitLine("SET I, 0") // Loop counter
PostLabel("pndiv") // Loop: divide A by 10 until 0 is left
PostLabel("printint1") // Loop: divide A by 10 until 0 is left
EmitLine("SET B, A") // Store A (number) for later
EmitLine("MOD A, 0xa") // Get remainder from division by 10
EmitLine("ADD A, 0x30") // Add 0x30 to the remainder to get ASCII code
EmitLine("SET PUSH, A") // Store the remainder (digit) on the stack
EmitLine("SET A, B") // Get A (number) back
EmitLine("DIV A, 0xa") // Divide the number by 10
EmitLine("ADD I, 1") // Increment loop counter
EmitLine("IFN A, 0") // A > 10: jump to :pnloop1
EmitLine("SET PC, pndiv")

PostLabel("pnprint") // Loop: print character by character
EmitLine("IFN A, 0") // A > 10: jump
EmitLine("SET PC, printint1")
PostLabel("printint2") // Loop: print character by character
EmitLine("SET A, POP") // Get digit from stack
EmitLine("SET B, X") // Get current cursor position
EmitLine("ADD B, 0x8000") // Add video mem address
EmitLine("SET [B], A") // Set video memory byte to show char
EmitLine("ADD X, 1") // Increment cursor position
EmitLine("IFN X, 0x160") // Check if we should do next line (X > 32)
EmitLine("SET PC, pnline")
EmitLine("SET X, 0") // First row, first column
PostLabel("pnline")
EmitLine("JSR printchar") // Print character
EmitLine("SUB I, 1") // Decrement loop counter
EmitLine("IFN I, 0")
EmitLine("SET PC, pnprint") // Jump back to :pnloop2 if there are more chars
EmitLine("SET PC, printint2") // Jump back if there are more chars
EmitLine("SET A, POP")
Ret()
}

func Print() {
Next()
BoolExpression()
EmitLine("JSR printnum")

EmitLine("JSR printint")
}

func Rem() {
Next()
for Look != '\n' {
GetChar()
}
Next()
}

func Block() {
Expand All @@ -720,6 +733,8 @@ func Block() {
Loc()
case 'p':
Print()
case 'r':
Rem()
default:
Assignment()
}
Expand All @@ -739,12 +754,19 @@ func Alloc() {

func Declarations() {
Scan()
currentStack := StackDepth
for Token == 'd' {
Alloc()
for Token == ',' {
Alloc()
}
}

// Allocate space on the stack for the vars
i := StackDepth - currentStack
if i > 0 {
EmitLine(fmt.Sprintf("SUB SP, %d ; Alloc space on stack", i))
}
}

func Init() {
Expand Down
5 changes: 4 additions & 1 deletion test.bas
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
DIM num, pad, test, big

REM Decide if whe should test bigger or smaller variable (0 or 1)
big = 1

REM With how many 0's we should pad the number
pad = 4
test = 1

IF big == 1 THEN
num = 32
ELSE
num = 9
END IF

test = 1

WHILE pad > 1
pad = pad - 1
test = test * 10
Expand Down

0 comments on commit 982fad5

Please sign in to comment.