Skip to content

Commit

Permalink
add return
Browse files Browse the repository at this point in the history
  • Loading branch information
MESYETI committed Apr 4, 2024
1 parent 7ce01d2 commit 5f5a544
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ out.asm
out.com
out.bin
test.cal
out.c
Binary file added a.out
Binary file not shown.
10 changes: 10 additions & 0 deletions examples/return.cal
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Flags:
Backends:
rm86 - Real mode x86
y16 - YETI-16 Mk2
";

int main(string[] args) {
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 0 additions & 12 deletions source/backends/c.d

This file was deleted.

16 changes: 16 additions & 0 deletions source/backends/rm86.d
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class BackendRM86 : CompilerBackend {
Constant[string] consts;
bool inScope;
Array[] arrays;
string thisFunc;

this() {
types["u8"] = Type(1);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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";
}
}
4 changes: 4 additions & 0 deletions source/backends/y16.d
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,8 @@ class BackendY16 : CompilerBackend {
override void CompileStruct(StructNode node) {
assert(0);
}

override void CompileReturn(WordNode node) {
assert(0);
}
}
13 changes: 12 additions & 1 deletion source/compiler.d
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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: {
Expand Down
10 changes: 6 additions & 4 deletions source/lexer.d
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 5f5a544

Please sign in to comment.