Skip to content

Commit

Permalink
add error statement
Browse files Browse the repository at this point in the history
  • Loading branch information
MESYETI committed May 31, 2024
1 parent f0fff8a commit 1419a46
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
2 changes: 1 addition & 1 deletion editors/micro_callisto.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ detect:
rules:
- statement: "\\b(func|end|begin|asm|include|inline|if|then|elseif|else|while|do)\\b"
- statement: "\\b(let|enable|requires|struct|version|return|const|enum|restrict)\\b"
- statement: "\\b(continue|break|union|alias)\\b"
- statement: "\\b(continue|break|union|alias|overwrite|error)\\b"
- type: "\\b(addr|void|u8|i8|u16|i16|u32|i32|u64|i64|size|usize|cell|array)\\b"

- constant.string:
Expand Down
10 changes: 10 additions & 0 deletions examples/error.cal
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
include "cores/select.cal"
include "std/io.cal"

# will print Hello, world! on RM86, will error on Linux86

version Linux86
error
end

"Hello, world!" printlstr
18 changes: 13 additions & 5 deletions source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Flags:
-na - Disables the -a flag
--version - Shows the callisto version
-dp - Prints parser output
-es - Export all Callisto symbols (and add util functions for interacting
with the Callisto stack)
Backends:
rm86 - Real mode x86 and MS-DOS
Expand Down Expand Up @@ -54,6 +56,7 @@ int main(string[] args) {
CompilerBackend backend = new BackendLinux86();
bool doDebug;
bool debugParser;
bool exportSymbols;

for (size_t i = 1; i < args.length; ++ i) {
if (args[i][0] == '-') {
Expand Down Expand Up @@ -158,6 +161,10 @@ int main(string[] args) {
debugParser = true;
break;
}
case "-es": {
exportSymbols = true;
break;
}
default: {
stderr.writefln("Unknown flag '%s'", args[i]);
return 1;
Expand Down Expand Up @@ -190,11 +197,12 @@ int main(string[] args) {
return 0;
}

auto compiler = new Compiler();
compiler.backend = backend;
compiler.backend.org = org;
compiler.backend.orgSet = orgSet;
compiler.backend.useDebug = doDebug;
auto compiler = new Compiler();
compiler.backend = backend;
compiler.backend.org = org;
compiler.backend.orgSet = orgSet;
compiler.backend.useDebug = doDebug;
compiler.backend.exportSymbols = exportSymbols;

versions ~= compiler.backend.GetVersions();

Expand Down
27 changes: 27 additions & 0 deletions source/backends/linux86.d
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ class BackendLinux86 : CompilerBackend {
// copy static array constants
output ~= "call __copy_arrays\n";

// create functions for interop
if (exportSymbols) {
output ~= "
global cal_push
cal_push:
mov [r15], rdi
add r15, 8
ret
cal_pop:
sub r15, 8
mov rax, [r15]
ret
";
}

// jump to main
output ~= "jmp __calmain\n";
}
Expand All @@ -186,10 +201,18 @@ class BackendLinux86 : CompilerBackend {

foreach (name, var ; globals) {
output ~= format("__global_%s: resb %d\n", name.Sanitise(), var.Size());

if (exportSymbols) {
output ~= format("global __global_%s\n", name.Sanitise());
}
}

foreach (i, ref array ; arrays) {
output ~= format("__array_%d: resb %d\n", i, array.Size());

if (exportSymbols) {
output ~= format("global __array_%d\n", i);
}
}

// create array source
Expand Down Expand Up @@ -280,6 +303,10 @@ class BackendLinux86 : CompilerBackend {

words[node.name] = Word(false, []);

if (exportSymbols) {
output ~= format("global __func__%s\n", node.name.Sanitise());
}

output ~= format("__func__%s:\n", node.name.Sanitise());

foreach (ref inode ; node.nodes) {
Expand Down
2 changes: 2 additions & 0 deletions source/compiler.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class CompilerBackend {
bool orgSet;
Compiler compiler;
bool useDebug;
bool exportSymbols;

abstract string[] GetVersions();
abstract string[] FinalCommands();
Expand Down Expand Up @@ -80,6 +81,7 @@ class Compiler {
case "return": backend.CompileReturn(node); break;
case "continue": backend.CompileContinue(node); break;
case "break": backend.CompileBreak(node); break;
case "error": backend.Error(node.error, "Error thrown by code"); break;
default: backend.CompileWord(node);
}
break;
Expand Down

0 comments on commit 1419a46

Please sign in to comment.