Skip to content

Commit

Permalink
feat: add str type
Browse files Browse the repository at this point in the history
  • Loading branch information
nawetimebomb committed Oct 8, 2024
1 parent 050f12d commit 61a482c
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 41 deletions.
24 changes: 11 additions & 13 deletions compiler/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,26 +339,20 @@ func newVariable(token Token, offset int) (Variable, int) {

newVar.word = parser.previous.value.(string)
newVar.offset = offset
newOffset = offset
// TODO: This should technically support bigger sizes for arrays
newOffset = offset + SIZE_64b

// TODO: Check for duplicated name

advance()
vt := parser.previous

switch vt.typ {
case TOKEN_BOOL:
newVar.dtype = DATA_BOOL
newOffset += SIZE_64b
case TOKEN_INT:
newVar.dtype = DATA_INT
newOffset += SIZE_64b
case TOKEN_PTR:
newVar.dtype = DATA_PTR
newOffset += SIZE_64b
case TOKEN_CHAR:
newVar.dtype = DATA_CHAR
newOffset += SIZE_8b
case TOKEN_BOOL: newVar.dtype = DATA_BOOL
case TOKEN_CHAR: newVar.dtype = DATA_CHAR
case TOKEN_INT: newVar.dtype = DATA_INT
case TOKEN_PTR: newVar.dtype = DATA_PTR
case TOKEN_STR: newVar.dtype = DATA_STR
default:
errorAt(&parser.previous, MsgParseVarMissingValue)
ExitWithError(CodeParseError)
Expand Down Expand Up @@ -821,6 +815,8 @@ func parseArityInAssembly(token Token, args *Arity) {
newArg.typ = DATA_INT
case TOKEN_PTR:
newArg.typ = DATA_PTR
case TOKEN_STR:
newArg.typ = DATA_STR
default:
msg := fmt.Sprintf(MsgParseTypeUnknown, token.value.(string))
errorAt(&token, msg)
Expand Down Expand Up @@ -849,6 +845,8 @@ func parseArityInFunction(token Token, function *Function, parsingArguments bool
newArg.typ = DATA_INT
case TOKEN_PTR:
newArg.typ = DATA_PTR
case TOKEN_STR:
newArg.typ = DATA_STR
case TOKEN_PARAPOLY:
if !parsingArguments {
errorAt(&token, MsgParseArityReturnParapolyNotAllowed)
Expand Down
20 changes: 15 additions & 5 deletions compiler/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ const (
// Types
TOKEN_ANY
TOKEN_BOOL
TOKEN_STARBOOL
TOKEN_CHAR
TOKEN_STARCHAR
TOKEN_INT
TOKEN_STARINT
TOKEN_PARAPOLY
TOKEN_PTR
TOKEN_STR
TOKEN_STARSTR

// Flow Control
TOKEN_CASE
Expand All @@ -49,7 +54,6 @@ const (
// NEW
TOKEN_DASH_DASH_DASH
TOKEN_LET
TOKEN_LETSTAR
TOKEN_IN
TOKEN_DONE

Expand Down Expand Up @@ -88,16 +92,24 @@ type reserved struct {
}

var reservedWords = []reserved{
// CONSTANTS
reserved{typ: TOKEN_CONSTANT_FALSE, word: "false" },
reserved{typ: TOKEN_CONSTANT_TRUE, word: "true" },

// DATA TYPES
reserved{typ: TOKEN_ANY, word: "any" },
reserved{typ: TOKEN_BOOL, word: "bool" },
reserved{typ: TOKEN_STARBOOL, word: "*bool" },
reserved{typ: TOKEN_CHAR, word: "char" },
reserved{typ: TOKEN_STARCHAR, word: "*char" },
reserved{typ: TOKEN_INT, word: "int" },
reserved{typ: TOKEN_STARINT, word: "*int" },
reserved{typ: TOKEN_PTR, word: "ptr" },
reserved{typ: TOKEN_STR, word: "str" },
reserved{typ: TOKEN_STARSTR, word: "*str" },

reserved{typ: TOKEN_DASH_DASH_DASH, word: "---" },
reserved{typ: TOKEN_LET, word: "let" },
reserved{typ: TOKEN_LETSTAR, word: "let*" },
reserved{typ: TOKEN_IN, word: "in" },
reserved{typ: TOKEN_DONE, word: "done" },

Expand All @@ -106,6 +118,7 @@ var reservedWords = []reserved{
reserved{typ: TOKEN_C_BANG, word: "c!" },
reserved{typ: TOKEN_C_AT, word: "c@" },

reserved{typ: TOKEN_ELSE, word: "else" },
reserved{typ: TOKEN_FI, word: "fi" },
reserved{typ: TOKEN_IF, word: "if" },
reserved{typ: TOKEN_LOOP, word: "loop" },
Expand All @@ -117,9 +130,7 @@ var reservedWords = []reserved{
reserved{typ: TOKEN_ASM, word: "asm" },
reserved{typ: TOKEN_BANG_EQUAL, word: "!=" },
reserved{typ: TOKEN_CONST, word: "const" },
reserved{typ: TOKEN_ELSE, word: "else" },
reserved{typ: TOKEN_EQUAL, word: "=" },
reserved{typ: TOKEN_CONSTANT_FALSE, word: "false" },
reserved{typ: TOKEN_FN, word: "fn" },
reserved{typ: TOKEN_GREATER, word: ">" },
reserved{typ: TOKEN_GREATER_EQUAL, word: ">=" },
Expand All @@ -134,7 +145,6 @@ var reservedWords = []reserved{
reserved{typ: TOKEN_SLASH, word: "/" },
reserved{typ: TOKEN_STAR, word: "*" },
reserved{typ: TOKEN_THIS, word: "this" },
reserved{typ: TOKEN_CONSTANT_TRUE, word: "true" },
reserved{typ: TOKEN_USING, word: "using" },
reserved{typ: TOKEN_VAR, word: "var" },
}
Expand Down
12 changes: 7 additions & 5 deletions compiler/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"reflect"
)

const STACK_SIZE = 10
const STACK_SIZE = 1024

type Typecheck struct {
stack [STACK_SIZE]DataType
Expand Down Expand Up @@ -44,6 +44,7 @@ func getDataTypeName(v DataType) string {
case DATA_INFER: r = "$type"
case DATA_INT: r = "int"
case DATA_PTR: r = "ptr"
case DATA_STR: r = "str"
case DATA_ANY: r = "any"
}
return r
Expand Down Expand Up @@ -121,6 +122,7 @@ func assertArgumentType(test []DataType, want []DataType, code Code, loc Locatio
}

if errFound {
fmt.Println(code)
msg := fmt.Sprintf(MsgTypecheckArgumentsTypeMismatch,
code.op, getDataTypeNames(test), getDataTypeNames(want))
ReportErrorAtLocation(msg, loc)
Expand Down Expand Up @@ -217,7 +219,7 @@ func ValidateRun() {
case OP_PUSH_INT:
tc.push(DATA_INT)
case OP_PUSH_STR:
tc.push(DATA_PTR)
tc.push(DATA_STR)
case OP_PUSH_VAR_GLOBAL:
tc.push(DATA_PTR)
// for _, v := range TheProgram.variables {
Expand Down Expand Up @@ -246,7 +248,7 @@ func ValidateRun() {
case OP_STORE:
b := tc.pop()
a := tc.pop()
assertArgumentType(dtArray(DATA_ANY, DATA_PTR), dtArray(a, b), code, loc)
assertArgumentType(dtArray(a, b), dtArray(DATA_ANY, DATA_PTR), code, loc)
case OP_STORE_CHAR:
b := tc.pop()
a := tc.pop()
Expand All @@ -256,12 +258,12 @@ func ValidateRun() {
)
case OP_LOAD:
a := tc.pop()
assertArgumentType(dtArray(DATA_PTR), dtArray(a), code, loc)
assertArgumentType(dtArray(a), dtArray(DATA_PTR), code, loc)
tc.push(DATA_INT)
case OP_LOAD_CHAR:
b := tc.pop()
a := tc.pop()
assertArgumentType(dtArray(DATA_INT, DATA_PTR), dtArray(a, b), code, loc)
assertArgumentType(dtArray(a, b), dtArray(DATA_INT, DATA_PTR), code, loc)
tc.push(DATA_CHAR)

case OP_LET_BIND:
Expand Down
6 changes: 3 additions & 3 deletions libs/strings.sk
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ fn len (int --- int) 0 dup rot < until ++ I 10 / loop ret
; - <string> len
; Returns:
; - The number of characters in teh string. "Stanczyk" returns 8
fn len (ptr --- int) let s in 0 0 dup s c@ != until ++ dup s c@ loop done ret
fn len (str --- int) let s in 0 0 dup s ptr c@ != until ++ dup s ptr c@ loop done ret

; Compares two strings and returns the results of the comparison
; Calling:
; - <str1> <str2> strequal
; Returns:
; - A boolean true if the arguments are equal, false if they are different.
fn strequal (ptr ptr --- bool)
fn strequal (str str --- bool)
over len swap dup len
let s1 l1 s2 l2 in
l1 l2 = dup if
0 l1 <= until
I s1 c@ I s2 c@ != if false nip leave fi
I s1 ptr c@ I s2 ptr c@ != if false nip leave fi
I -- loop
fi
done
Expand Down
22 changes: 11 additions & 11 deletions libs/unix.sk
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ const O_CREATE 64
; Helper function to access the arguments Array (passed through when calling
; the program).
; Calling:
; - <index> argv[]
; - <index> argv-array
; Returns:
; - A pointer to the element in the Array. If element is out of reach,
; throws an error and stops execution.
fn argv-array (int --- ptr)
8 * argv int + ptr @
fn argv-array (int --- str)
8 * argv int + ptr @ str
dup int 0 = if
dup print
"argv empty" panic
Expand All @@ -87,9 +87,9 @@ fn clear () "\e[H\e[2J" print ret
; Helper function to print text on the standard error output for the system.
; Calling:
; - <string> eputs
fn eputs (ptr)
fn eputs (str)
dup len swap
asm int ptr (
asm int str (
mov rax, SYS_WRITE
mov rdi, STDERR
pop rsi
Expand All @@ -113,7 +113,7 @@ ret
; program after printing the message.
; Calling:
; - <string> panic
fn panic (ptr)
fn panic (str)
" ERROR: " eputs
eputs
"\n" eputs
Expand All @@ -123,9 +123,9 @@ ret
; Helper function to print a string on the standard output.
; Calling:
; - <string> print
fn print (ptr)
fn print (str)
dup len swap
asm int ptr (
asm int str (
mov rax, SYS_WRITE
mov rdi, STDOUT
pop rsi
Expand All @@ -138,7 +138,7 @@ ret
; Calling:
; - <char> print
fn print (char)
var buffer ptr
var buffer str
take int &buffer itoa
buffer print
ret
Expand All @@ -147,7 +147,7 @@ ret
; Calling:
; - <int> print
fn print (int)
var buffer ptr
var buffer str
take &buffer itoa
buffer print
ret
Expand All @@ -160,7 +160,7 @@ fn print (bool) take 1 = if "true" print else "false" print fi ret
; Helper function internally calling "print", but adds a newline after printing.
; Calling:
; - <ptr> println
fn println (ptr) print "\n" print ret
fn println (str) print "\n" print ret

; Helper function internally calling "print", but adds a newline after printing.
; Calling:
Expand Down
4 changes: 2 additions & 2 deletions tests/assembly.sk
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using io

fn custom-linux-write (ptr int)
fn custom-linux-write (str int)
swap
asm int ptr (
asm int str (
mov rax, SYS_WRITE
mov rdi, STDOUT
pop rsi
Expand Down
2 changes: 1 addition & 1 deletion tests/polymorphism.sk
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn equal (bool bool --- bool)
=
ret

fn equal (ptr ptr --- bool)
fn equal (str str --- bool)
strequal
ret

Expand Down
2 changes: 1 addition & 1 deletion tests/strequal.sk
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using io

fn get-str (--- ptr)
fn get-str (--- str)
"My test string"
ret

Expand Down

0 comments on commit 61a482c

Please sign in to comment.