Skip to content

Commit

Permalink
feat: uniform closing and opening statements with parens instead of "…
Browse files Browse the repository at this point in the history
…do" and "."
  • Loading branch information
nawetimebomb committed Sep 21, 2024
1 parent b88e250 commit f3aac39
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 55 deletions.
47 changes: 26 additions & 21 deletions compiler/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,30 +152,35 @@ func newConstant(token Token) {
errorAt(&token, msg)
}

for !check(TOKEN_PAREN_CLOSE) && !check(TOKEN_EOF) {
advance()
tokens = append(tokens, parser.previous)
}
if match(TOKEN_PAREN_OPEN) {
for !check(TOKEN_PAREN_CLOSE) && !check(TOKEN_EOF) {
advance()
tokens = append(tokens, parser.previous)
}

if len(tokens) > 3 || len(tokens) == 2 {
msg := fmt.Sprintf(MsgParseConstInvalidContent, constant.word)
errorAt(&token, msg)
return
} else if len(tokens) == 3 {
left := tokens[0].value.(int)
right := tokens[1].value.(int)
if len(tokens) > 3 || len(tokens) == 2 {
msg := fmt.Sprintf(MsgParseConstInvalidContent, constant.word)
errorAt(&token, msg)
return
} else if len(tokens) == 3 {
left := tokens[0].value.(int)
right := tokens[1].value.(int)

switch tokens[2].typ {
case TOKEN_PLUS: constant.value = left + right
case TOKEN_STAR: constant.value = left * right
switch tokens[2].typ {
case TOKEN_PLUS: constant.value = left + right
case TOKEN_STAR: constant.value = left * right
}
} else if len(tokens) == 1 {
constant.value = tokens[0].value.(int)
}
} else if len(tokens) == 1 {
constant.value = tokens[0].value.(int)

consume(TOKEN_PAREN_CLOSE, MsgParseConstMissingDot)
} else {
advance()
constant.value = parser.previous.value.(int)
}

frontend.constants = append(frontend.constants, constant)

consume(TOKEN_PAREN_CLOSE, MsgParseConstMissingDot)
}

func newFunction(token Token) {
Expand Down Expand Up @@ -329,8 +334,6 @@ func newReserve(token Token) {
}

frontend.memories = append(frontend.memories, memory)

consume(TOKEN_PAREN_CLOSE, MsgParseReserveMissingDot)
}

func compile(index int) {
Expand Down Expand Up @@ -405,6 +408,8 @@ func addWord(word string) {
func addBind(token Token) {
startingBoundIndex := len(frontend.bindings)

consume(TOKEN_PAREN_OPEN, MsgParseBindMissingOpenStmt)

for match(TOKEN_WORD) {
t := parser.previous
frontend.bindings = append(frontend.bindings, Bound{
Expand All @@ -419,7 +424,7 @@ func addBind(token Token) {
return
}

consume(TOKEN_PAREN_CLOSE, MsgParseBindMissingDot)
consume(TOKEN_PAREN_CLOSE, MsgParseBindMissingCloseStmt)
emit(Code{op: OP_BIND, loc: token.loc, value: len(frontend.bindings)})
}

Expand Down
12 changes: 8 additions & 4 deletions compiler/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ const (
"missing or invalid bind content\n" +
"\t\tbind a b c .\n" +
"\t\t ^^^^^"
MsgParseBindMissingDot =
"missing '.'\n" +
"\t\tbind a b c .\n" +
"\t\t ^"
MsgParseBindMissingCloseStmt =
"missing ')'\n" +
"\t\tbind ( a b c )\n" +
"\t\t ^"
MsgParseBindMissingOpenStmt =
"missing '('\n" +
"\t\tbind ( a b c )\n" +
"\t\t ^"

MsgParseDoOrphanTokenFound =
"only use 'do' when starting a block statement\nE.g.:\n\tif [condition] do [...] else [...] .\n\t^^ ^^\n'do' can be used in other blocks like function and loops"
Expand Down
22 changes: 11 additions & 11 deletions libs/basics.sk
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@
;

; const MIN_INT8 -128 .
const MAX_INT8 127 )
const MAX_INT8 127

; const MIN_INT16 -32768 .
const MAX_INT16 32767 )
const MAX_INT16 32767

; const MIN_INT32 -2147483648 .
const MAX_INT32 2147483647 )
const MAX_INT32 2147483647

; const MIN_INT64 -9223372036854775808 .
const MAX_INT64 9223372036854775807 )
const MAX_INT64 9223372036854775807

const MAX_UINT8 255 )
const MAX_UINT16 65535 )
const MAX_UINT32 4294967295 )
const MAX_UINT64 18446744073709551615 )
const MAX_UINT8 255
const MAX_UINT16 65535
const MAX_UINT32 4294967295
const MAX_UINT64 18446744073709551615

fn and bool bool -> bool (
swap (int)
Expand Down Expand Up @@ -110,7 +110,7 @@ fn strlen ptr -> int (

fn str= ptr ptr -> bool (
over strlen swap dup strlen
bind s1 l1 s2 l2 )
bind ( s1 l1 s2 l2 )

false
if l1 l2 = (
Expand All @@ -132,8 +132,8 @@ fn intlen int -> int (
) drop
)

const TO_STRING_CAP 19 )
reserve TO_STRING_MEM TO_STRING_CAP )
const TO_STRING_CAP 19
reserve TO_STRING_MEM TO_STRING_CAP

fn int->str int -> ptr (
0 loop dup TO_STRING_CAP <= (
Expand Down
26 changes: 13 additions & 13 deletions libs/io.sk
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@
;; TODO: Add descriptions

; File descriptor (fd)
const STDIN 0 )
const STDOUT 1 )
const STDERR 2 )

const LINUX_READ 0 )
const LINUX_WRITE 1 )
const LINUX_EXIT 60 )

const READ 0 )
const WRITE 1 )
const READWRITE 2 )
const CREATE 64 )
const TRUNCATE 512 )
const STDIN 0
const STDOUT 1
const STDERR 2

const LINUX_READ 0
const LINUX_WRITE 1
const LINUX_EXIT 60

const READ 0
const WRITE 1
const READWRITE 2
const CREATE 64
const TRUNCATE 512

fn []argv int -> ptr (
8 * argv (int) + (ptr) ->64
Expand Down
2 changes: 1 addition & 1 deletion tests/bindings.sk
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using io
fn main (
"testing bindings to form a word" println
"S" "t" "a" "ń" "c" "z" "i" "k"
bind s t a n c z y k )
bind ( s t a n c z y k )
"Stańczyk " print
s print t print a print n print c print z print y print k println
)
6 changes: 3 additions & 3 deletions tests/constants.sk
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ using "io"
;; Description:
; making sure consts are evaluated correctly

const thirty 30 )
const +2010 20 10 + )
const *103 10 3 * )
const thirty 30
const +2010 ( 20 10 + )
const *103 ( 10 3 * )

fn main (
"making sure all these values are 30" println
Expand Down
2 changes: 1 addition & 1 deletion tests/memory.sk
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using io

reserve number 32 )
reserve number 32

fn main (
"Save the number to static memory and print it" println
Expand Down
2 changes: 1 addition & 1 deletion tests/recursion.sk
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using io
;; Test: recursion
;; Description:
; testing recusive functions
const ORIGINAL 10 )
const ORIGINAL 10

fn add-3-to-original int -> int (
if dup ORIGINAL 3 + = (
Expand Down

0 comments on commit f3aac39

Please sign in to comment.