From 58340776080c48607ef8e368d6293d6c82b03da2 Mon Sep 17 00:00:00 2001 From: jusax23 Date: Mon, 16 May 2022 17:45:34 +0200 Subject: [PATCH] while loop --- compiler/output/loops.as | 79 +++++++++++++++++++++++++++++++++++++ compiler/output/loops.b16 | 1 + compiler/scripts/loops.lisp | 7 ++++ compiler/tools/lispToAs.js | 31 +++++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 compiler/output/loops.as create mode 100644 compiler/output/loops.b16 create mode 100644 compiler/scripts/loops.lisp diff --git a/compiler/output/loops.as b/compiler/output/loops.as new file mode 100644 index 0000000..e267044 --- /dev/null +++ b/compiler/output/loops.as @@ -0,0 +1,79 @@ + +v0 = 0;4 +SHS 4 +;code + + ;defvar: executing value: + + ;num: Loading num + LIA 0 + + ;defvar: Store Value + STA 4 v0 + +beforloop2: + + ;<: execute first + + ;var: load Variable + LDA 4 v0 + + PSH 4 + ;<: execute secound + + ;num: Loading num + LIA 65535 + + CAB + PUL 4 + CMP 0 0 + LIA 1 + JS .endcompare1 + LIA 0 +endcompare1: + + LIB 1 + CMP 0 0 + JNE .afterloop3 +;loop code + + + ;print: executing value: + + ;var: load Variable + LDA 4 v0 + + ;print Value + OUT 0 + + ;let: executing value: + + ;+: next value + + ;var: load Variable + LDA 4 v0 + + + PSH 4 + + ;+: next value + + ;num: Loading num + LIA 1 + + + ;+: Pull/Add next + CAB + PUL 4 + ADD 0 0 0 + + ;let: Store Value + STA 4 v0 + + + JMP .beforloop2 +afterloop3: + +HLT +;functions + diff --git a/compiler/output/loops.b16 b/compiler/output/loops.b16 new file mode 100644 index 0000000..3ec41b7 --- /dev/null +++ b/compiler/output/loops.b16 @@ -0,0 +1 @@ +fe00040f00000000080400000104000039040f0000ffff133a043100000f0000000137002a0f00000000100000000131000034005401040000fd000104000039040f00000001133a04220000000804000032000cff \ No newline at end of file diff --git a/compiler/scripts/loops.lisp b/compiler/scripts/loops.lisp new file mode 100644 index 0000000..f60c8d3 --- /dev/null +++ b/compiler/scripts/loops.lisp @@ -0,0 +1,7 @@ + +(defvar i:uint32 0) + +(loop while (< i 0xffff) + (print i) + (let i (+ i 1)) +) diff --git a/compiler/tools/lispToAs.js b/compiler/tools/lispToAs.js index 16f2a8e..9210906 100644 --- a/compiler/tools/lispToAs.js +++ b/compiler/tools/lispToAs.js @@ -558,6 +558,37 @@ endcompare${id}: LIA 0 endcompare${id}: `; + }else if(data[0]=="loop"){ + if(data[1] == "while"){ + let [c,etype] = execute(data[2],"bool",context,local); + var idbefor = nid++; + var idafter = nid++; + var codeinsert = ""; + + for (var i = 3; i < data.length; i++) { + let [ci,ti] = execute(data[i],i==data.length-1?expect:"any",context,local); + ptype = ti; + codeinsert+=ci; + } + code+=` +beforloop${idbefor}: + ${c} + LIB 1 + CMP 0 0 + JNE .afterloop${idafter} +;loop code + +${codeinsert} + + JMP .beforloop${idbefor} +afterloop${idafter}: + `; + + }else if(data[1] == "for"){ + error("Methode 'for' will be implemented later.",...data[1].pos); + }else{ + error(`Unknown loop Methode: ${data[1]}`,...data[1].pos); + } }else{ if(extrafuns[data[0]]){ var fun = extrafuns[data[0]];