duskos

dusk os fork
git clone git://git.alexwennerberg.com/duskos
Log | Files | Refs | README | LICENSE

commit 5464abd5a9247450c6233898d335a46e018e2f96
parent 5cd2b2147d00534b662bf4c3307666b8309021ea
Author: Virgil Dupras <hsoft@hardcoded.net>
Date:   Fri, 10 Jun 2022 15:03:44 -0400

cc: function calls can have arguments

Diffstat:
Mfs/cc/ast.fs | 16+++++++++++-----
Mfs/cc/gen.fs | 9+++++++++
Mfs/cc/map.fs | 7+++----
Mfs/tests/cc.fs | 1+
Mfs/tests/test.c | 3+++
5 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/fs/cc/ast.fs b/fs/cc/ast.fs @@ -128,14 +128,20 @@ ASTIDCNT wordtbl astdatatbl ( node -- node ) \ Parse words \ parse a constant, variable or function call -: parseFactor ( tok -- node ) +: parseFactor ( tok -- node-or-0 ) dup isIdent? if \ Variable or FunCall _nextt ( prevtok newtok ) dup S" (" s= if \ FunCall - drop AST_FUNCALL createnode swap , _nextt ')' expectChar + drop AST_FUNCALL createnode swap , begin ( node ) + _nextt dup parseFactor ?dup if \ an argument + nip over addnode + _nextt dup S" ," s= if drop else to nexttputback then 0 + else \ not an argument + ')' expectChar 1 then until ( node ) else \ Variable to nexttputback AST_VARIABLE createnode swap , then else \ Constant - expectConst AST_CONSTANT createnode swap , then ; + parse if AST_CONSTANT createnode swap , else 0 then + then ; \ An expression can be 3 things: \ 1. a factor @@ -148,13 +154,13 @@ ASTIDCNT wordtbl astdatatbl ( node -- node ) over addnode ( node ) else ( tok ) \ binaryop or factor \ tok is expected to be a factor - parseFactor _nextt ( factor nexttok ) + parseFactor ?dup _assert _nextt ( factor nexttok ) dup bopid if ( factor tok binop ) nip ( factor binop ) AST_BINARYOP createnode swap , ( factor node ) tuck addnode _nextt ( binnode tok ) \ now, let's consume tokens as long as we have binops coming. begin ( bn tok ) - parseFactor _nextt ( bn factor tok ) dup bopid if ( bn fn tok bopid ) + parseFactor ?dup _assert _nextt ( bn factor tok ) dup bopid if ( bn fn tok bopid ) nip AST_BINARYOP createnode swap , ( bn1 fn bn2 ) \ another binop! who will get fn? bn1 or bn2? the one that has the \ best precedence! diff --git a/fs/cc/gen.fs b/fs/cc/gen.fs @@ -104,9 +104,18 @@ ASTIDCNT wordtbl gentbl ( node -- ) swap getfuncnode findvarinmap ( varnode ) data2 ( offset ) eax [ebp]+ mov, ; :w ( FunCall ) + \ pass arguments + dup firstchild ?dup if begin ( argnode ) + dup gennode + ebp 4 i32 sub, + [ebp] eax mov, + nextsibling ?dup not until then + \ find in map data1 ( name ) curmap findfuncinmap ?dup not if _err then ( mapfunc ) + \ call! data4 ( addr ) call, + \ get result eax [ebp] mov, ebp 4 i32 add, ; diff --git a/fs/cc/map.fs b/fs/cc/map.fs @@ -18,11 +18,10 @@ create mapidnames 4 c, ," unit" 8 c, ," function" 3 c, ," var" 0 value activenode \ node we're currently adding to : _err ( -- ) abort" mapping error" ; -: newnode - createnode dup activenode addnode ( node ) to activenode ; - : Unit ( -- ) MAP_UNIT createnode dup to curmap to activenode ; -: Function ( name -- ) MAP_FUNCTION newnode , 0 , 0 , 0 , ; +: Function ( name -- ) + MAP_FUNCTION createnode dup curmap addnode to activenode + ( name ) , 0 , 0 , 0 , ; : Variable ( offset name -- ) MAP_VARIABLE createnode activenode addnode , , ; diff --git a/fs/tests/cc.fs b/fs/tests/cc.fs @@ -12,4 +12,5 @@ boolops 0 #eq variables 42 #eq funcall 42 #eq 2 3 adder 5 #eq +42 plusone 43 #eq testend diff --git a/fs/tests/test.c b/fs/tests/test.c @@ -22,3 +22,6 @@ int funcall() { int adder(int a, int b) { return a + b; } +int plusone(int x) { + return adder(x, 1); +}