From 5f5a544a210f4ccda8cc42aba5ff1724fc437912 Mon Sep 17 00:00:00 2001 From: MESYETI Date: Thu, 4 Apr 2024 19:08:20 +0100 Subject: [PATCH] add return --- .gitignore | 1 + a.out | Bin 0 -> 15632 bytes examples/return.cal | 10 ++++++++++ source/app.d | 3 ++- source/backends/c.d | 12 ------------ source/backends/rm86.d | 16 ++++++++++++++++ source/backends/y16.d | 4 ++++ source/compiler.d | 13 ++++++++++++- source/lexer.d | 10 ++++++---- 9 files changed, 51 insertions(+), 18 deletions(-) create mode 100755 a.out create mode 100644 examples/return.cal delete mode 100644 source/backends/c.d diff --git a/.gitignore b/.gitignore index f856745..f8a5561 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ out.asm out.com out.bin test.cal +out.c diff --git a/a.out b/a.out new file mode 100755 index 0000000000000000000000000000000000000000..687343a5b20b07d1202cb7fa7168128f6be98029 GIT binary patch literal 15632 zcmeHOYit}>6~4Q+6Za8&6D5tCgk+Q^$OSxpC6?RBWc^rY3{GMahj*B)ch~kd_O81g z7pLM8)lkVYmeM~UmHtS7fdoaMB2htwoVaQm6r>b>5b>v`5Xpf6t_YP&)ePs%J>Pgd zb)t&b4>VVr`R@JBIrpA9_wMYSowHv}^>)W&F@;j5KB~CYm(Wa71n2fkg&?U~)do86 zR3A|5s9o-HTAtKEQeCdbS7W8{WrS>3!<98~ho%(}9x}4s?aFuAAu2#6*THrbERc2> zzbt+NA@|2K*qDmpoqfD1O<>TDUAa=H6|S_c#!t(5=Qc?P;uq6ww;*;4V&@|}#BxEz zlVie1tHd*mNs@6%dS|7=c9UWk#xBtZ@)%r>Uvskm%6NTZS8jOSs4(u+v;tzhPmrBV zt!UgCFwMtgyxYw~gUka^!m8>=@&hgV>PND5Bl*I_WZh(IOI^!8bG&Fac>}Xw4Rr9D zI&|cOil0?;%)vgUVyoJ5X_JaqSa&lW9Co&U{pQ8eTQ5GA-&9yX(B}UB0i%8ALDu2x z;67Q03v+y+unY&d_k+Cbu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cal b/examples/return.cal new file mode 100644 index 0000000..12cc57e --- /dev/null +++ b/examples/return.cal @@ -0,0 +1,10 @@ +include "cores/select.cal" +include "std/io.cal" + +func returnExample begin + "Hello 1\r\n" printlstr + return + "Hello 2\r\n" printlstr +end + +returnExample diff --git a/source/app.d b/source/app.d index 3dd810a..d298630 100644 --- a/source/app.d +++ b/source/app.d @@ -25,6 +25,7 @@ Flags: Backends: rm86 - Real mode x86 + y16 - YETI-16 Mk2 "; int main(string[] args) { @@ -56,7 +57,7 @@ int main(string[] args) { stderr.writeln("-o requires FILE parameter"); return 1; } - if (outFile != "") { + if (outFile != "out.asm") { stderr.writeln("Output file set multiple times"); return 1; } diff --git a/source/backends/c.d b/source/backends/c.d deleted file mode 100644 index 5f419fa..0000000 --- a/source/backends/c.d +++ /dev/null @@ -1,12 +0,0 @@ -module callisto.backends.c; - -import std.conv; -import std.stdio; -import std.range; -import std.format; -import std.algorithm; -import callisto.util; -import callisto.error; -import callisto.parser; -import callisto.compiler; -import callisto.language; diff --git a/source/backends/rm86.d b/source/backends/rm86.d index e3fd6fa..41d3687 100644 --- a/source/backends/rm86.d +++ b/source/backends/rm86.d @@ -58,6 +58,7 @@ class BackendRM86 : CompilerBackend { Constant[string] consts; bool inScope; Array[] arrays; + string thisFunc; this() { types["u8"] = Type(1); @@ -185,6 +186,8 @@ class BackendRM86 : CompilerBackend { Error(node.error, "Name '%s' can't be used", node.name); } + thisFunc = node.name; + if (node.inline) { words[node.name] = Word(true, node.nodes); } @@ -437,4 +440,17 @@ class BackendRM86 : CompilerBackend { NewConst(format("%s.sizeof", node.name), offset); types[node.name] = Type(offset); } + + override void CompileReturn(WordNode node) { + if (!inScope) { + Error(node.error, "Return used outside of function"); + } + + size_t scopeSize; + foreach (ref var ; variables) { + scopeSize += var.Size(); + } + output ~= format("add sp, %d\n", scopeSize); + output ~= "ret\n"; + } } diff --git a/source/backends/y16.d b/source/backends/y16.d index 76f0996..8ed771f 100644 --- a/source/backends/y16.d +++ b/source/backends/y16.d @@ -305,4 +305,8 @@ class BackendY16 : CompilerBackend { override void CompileStruct(StructNode node) { assert(0); } + + override void CompileReturn(WordNode node) { + assert(0); + } } diff --git a/source/compiler.d b/source/compiler.d index 0387d72..419cd53 100644 --- a/source/compiler.d +++ b/source/compiler.d @@ -29,6 +29,7 @@ class CompilerBackend { abstract void CompileArray(ArrayNode node); abstract void CompileString(StringNode node); abstract void CompileStruct(StructNode node); + abstract void CompileReturn(WordNode node); final void Error(Char, A...)(ErrorInfo error, in Char[] fmt, A args) { ErrorBegin(error); @@ -61,7 +62,17 @@ class Compiler { void CompileNode(Node inode) { switch (inode.type) { case NodeType.Word: { - backend.CompileWord(cast(WordNode) inode); + auto node = cast(WordNode) inode; + + switch (node.name) { + case "return": { + backend.CompileReturn(node); + break; + } + default: { + backend.CompileWord(node); + } + } break; } case NodeType.Integer: { diff --git a/source/lexer.d b/source/lexer.d index 85bb2aa..cb5a10d 100644 --- a/source/lexer.d +++ b/source/lexer.d @@ -125,10 +125,12 @@ class Lexer { void Lex() { char[char] escapes = [ - 'n': '\n', - 'r': '\r', - 't': '\t', - 'e': '\x1b' + 'n': '\n', + 'r': '\r', + 't': '\t', + 'e': '\x1b', + '"': '"', + '\\': '\\' ]; for (i = 0; i < code.length; ++ i) {