Skip to content

Commit

Permalink
added negative numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
bblhd committed Dec 13, 2022
1 parent a50b428 commit d9b1708
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 49 deletions.
22 changes: 16 additions & 6 deletions compile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function functionHeader(keyword, tokens)
if keyword == "public" or keyword == "private" then
target.functionDefinition(name, allocatedCount, vararg and namedArgumentCount)
tokens.assert(statement(tokens), "definition of function '"..name.."' is invalid")
target.numlit('0')
target.numlit(0)
target.ret()
end
return true
Expand Down Expand Up @@ -507,19 +507,29 @@ function tokeniser(path)
removeJunk()
local token = nil
if program:match("^'\\.'") then
token = tostring(escapes[program:sub(3,3)] or string.byte(program,3))
token = escapes[program:sub(3,3)] or string.byte(program,3)
program = program:sub(5)
elseif program:match("^'.'") then
token = tostring(string.byte(program, 2))
token = string.byte(program, 2)
program = program:sub(4)
elseif program:match('^0x[0-9a-fA-F]') then
token, program = program:match("^0x([0-9a-f]+)(.*)$")
token = tostring(tonumber(token, 16))
token, program = program:match("^0x([0-9a-fA-F]+)(.*)$")
token = tonumber(token, 16)
elseif program:match('^0b[01]') then
token, program = program:match("^0b([01]+)(.*)$")
token = tostring(tonumber(token, 2))
token = tonumber(token, 2)
elseif program:match('^[0-9]') then
token, program = program:match("^([0-9]+)(.*)$")
token = tonumber(token)
elseif program:match('^%-0x[0-9a-fA-F]') then
token, program = program:match("^%-0x([0-9a-fA-F]+)(.*)$")
token = -tonumber(token, 16)
elseif program:match('^%-0b[01]') then
token, program = program:match("^%-0b([01]+)(.*)$")
token = -tonumber(token, 2)
elseif program:match('^%-[0-9]') then
token, program = program:match("^%-([0-9]+)(.*)$")
token = -tonumber(token)
end
return token
end
Expand Down
6 changes: 3 additions & 3 deletions docs/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ functionCall = "(" name (expression)* ")"
name = [a-zA-Z_][a-zA-Z0-9_]*
number = [0-9]+
| "0x" [0-9a-fA-F]+
| "0b" [01]+
number = "-"? [0-9]+
| "-"? "0x" [0-9a-fA-F]+
| "-"? "0b" [01]+
| "'" (ANY | BACKSLASH ANY) "'"
string = '"' (ANY - '"' | BACKSLASH ANY)* '"'
Expand Down
19 changes: 16 additions & 3 deletions linux_x86_32/target.lua
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,19 @@ local builtins = {

['syscall'] = {required = 1, moreAllowed = true, f=function(n)
text("mov eax, [esp]")
local registers = {"ebx", "ecx", "edx", "esi", "edi"}
local registers = {"ebx", "ecx", "edx", "esi", "edi", "ebp"}
for i=1,n-1 do
text("mov "..registers[i]..", [esp"..offsetString(i).."]")
if i+1 == 7 then
text("push ebp")
text("mov ebp, [esp"..offsetString(i+1).."]")
else
text("mov "..registers[i]..", [esp"..offsetString(i).."]")
end
end
text("int 80h")
if n == 7 then
text("pop ebp")
end
end},

['load8'] = {required = 1, moreAllowed = false, f=function(n)
Expand Down Expand Up @@ -287,7 +295,12 @@ local function as_return()
end

local function as_numlit(value)
text("mov eax, "..value)
if value >= 0 then
text("mov eax, "..value)
else
text("mov eax, "..(-value))
text("neg eax")
end
end

oldstrings = {}
Expand Down
7 changes: 6 additions & 1 deletion linux_x86_64/target.lua
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,12 @@ local function as_return()
end

local function as_numlit(value)
text("mov rax, "..value)
if value >= 0 then
text("mov rax, "..value)
else
text("mov rax, "..(-value))
text("neg rax")
end
end

oldstrings = {}
Expand Down
2 changes: 1 addition & 1 deletion macos_x86_64/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ FILE_SRC="$1"
FILE_OBJ="$NAME.o"
FILE_DST="bin/$NAME"

if lua compile.lua -i $FILE_SRC -o $FILE_OBJ -L 'system=./src/sys_bsd_x86_64.fen' -l './src' -p 'macos_x86_64'; then
if lua compile.lua -i $FILE_SRC -o $FILE_OBJ -L 'system=./src/sys_macos_x86_64.fen' -l './src' -p 'macos_x86_64'; then
mkdir -p bin
nasm -f macho64 macos_x86_64/entry.s -o entry.o
ld -macosx_version_min 10.6 -dead_strip_dylibs -o $FILE_DST *.o
Expand Down
7 changes: 6 additions & 1 deletion macos_x86_64/target.lua
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,12 @@ local function as_return()
end

local function as_numlit(value)
text("mov rax, "..value)
if value >= 0 then
text("mov rax, "..value)
else
text("mov rax, "..(-value))
text("neg rax")
end
end

oldstrings = {}
Expand Down
4 changes: 1 addition & 3 deletions src/hello_world.fen
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
include "nonstdio"

intern (printi2thesquequal n)

public (main argc argv...) {
(print "Hello World!")
(print "Hello World!\n")
return 0
}
19 changes: 9 additions & 10 deletions src/nonstdio.fen
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ include "system"

intern (strlen string)
intern (print string)
intern (printi number base)
intern (printu number base)
intern (scan string n)
intern (printf format argc...)

Expand All @@ -26,24 +26,23 @@ private (print string) {
return (syscall SYS_WRITE 1 string (strlen string))
}

private printi_buffer[32]
private printu_buffer[64]

private (printi n base; buffer rem) {
buffer := (add printi_buffer 32)
(storeByte {buffer := (sub buffer 1)} 0)
private (printu n base; buffer rem) {
buffer := (add printu_buffer 64)

while (gt n 0) {
while (ne n 0) {
rem := (mod n base)
rem := (add rem {if (lt rem 10) 48 else 55})

if (gt buffer printi_buffer) {
if (gt buffer printu_buffer) {
(storeByte {buffer := (sub buffer 1)} rem)
}

n := (div n base)
}

(syscall SYS_WRITE 1 buffer (sub (add printi_buffer 32) buffer))
(syscall SYS_WRITE 1 buffer (sub (add printu_buffer 64) buffer))
}

private (scan buffer n; char) {
Expand All @@ -69,8 +68,8 @@ private (printf format args...) {
if (eq (loadByte format) 's') {
(print (loadWord args))
args := (add args WORD_SIZE)
} else if (eq (loadByte format) 'i') {
(printi (loadWord args) 10)
} else if (eq (loadByte format) 'u') {
(printu (loadWord args) 10)
args := (add args WORD_SIZE)
} else if (eq (loadByte format) 'c') {
(syscall SYS_WRITE 1 args 1)
Expand Down
4 changes: 2 additions & 2 deletions src/stress_test.fen
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include "nonstdio"
intern (printHexdump path)

public (main argc argv...; name answer) {
(printf "Test int: %i\nTest string: \"%s\"\nTest char: '%c'\n"
(printf "Test int: %u\nTest string: \"%s\"\nTest char: '%c'\n"
13
"Hello World!"
0x43
Expand All @@ -16,7 +16,7 @@ public (main argc argv...; name answer) {
(printf "Hello and welcome to fennec, %s!\n" name)

if (gt argc 1) {
(printf "I was given %i arguments.\n" (sub argc 1))
(printf "I was given %u arguments.\n" (sub argc 1))
(printf "The first argument I was given was '%s'.\n" (loadWord (add argv WORD_SIZE)))
} else {
(printf "Nothing was passed to me as an argument. :(\n")
Expand Down
9 changes: 0 additions & 9 deletions src/sys_bsd_x86_64.fen

This file was deleted.

30 changes: 25 additions & 5 deletions src/sys_linux_x86_32.fen
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
constant O_RDONLY = 0
constant O_WRONLY = 1
constant O_RDWR = 2

constant SYS_EXIT = 1


constant SYS_READ = 3
constant SYS_WRITE = 4


constant SYS_OPEN = 5
constant SYS_CLOSE = 6
constant SYS_CLOSE = 6

constant O_RDONLY = 0
constant O_WRONLY = 1
constant O_RDWR = 2


constant SYS_MMAP = 192
constant SYS_MUNMAP = 91

constant PROT_NONE = 0
constant PROT_READ = 1
constant PROT_WRITE = 2
constant PROT_EXEC = 4

constant MAP_SHARED = 1
constant MAP_PRIVATE = 2
constant MAP_FILE = 0
constant MAP_ANONYMOUS = 0x1000

constant MAP_FAILED = -1
30 changes: 25 additions & 5 deletions src/sys_linux_x86_64.fen
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
constant O_RDONLY = 0
constant O_WRONLY = 1
constant O_RDWR = 2

constant SYS_EXIT = 60


constant SYS_READ = 0
constant SYS_WRITE = 1


constant SYS_OPEN = 2
constant SYS_CLOSE = 3
constant SYS_CLOSE = 3

constant O_RDONLY = 0
constant O_WRONLY = 1
constant O_RDWR = 2


constant SYS_MMAP = 9
constant SYS_MUNMAP = 11

constant PROT_NONE = 0
constant PROT_READ = 1
constant PROT_WRITE = 2
constant PROT_EXEC = 4

constant MAP_SHARED = 1
constant MAP_PRIVATE = 2
constant MAP_FILE = 0
constant MAP_ANONYMOUS = 0x1000

constant MAP_FAILED = -1
29 changes: 29 additions & 0 deletions src/sys_macos_x86_64.fen
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
constant SYS_EXIT = 0x2000001


constant SYS_READ = 0x2000003
constant SYS_WRITE = 0x2000004


constant SYS_OPEN = 0x2000005
constant SYS_CLOSE = 0x2000006

constant O_RDONLY = 0
constant O_WRONLY = 1
constant O_RDWR = 2


constant SYS_MMAP = 0x20000C5
constant SYS_MUNMAP = 0x2000049

constant PROT_NONE = 0
constant PROT_READ = 1
constant PROT_WRITE = 2
constant PROT_EXEC = 4

constant MAP_SHARED = 1
constant MAP_PRIVATE = 2
constant MAP_FILE = 0
constant MAP_ANONYMOUS = 0x1000

constant MAP_FAILED = -1

0 comments on commit d9b1708

Please sign in to comment.