Skip to content

Commit

Permalink
make const expressions unable to overwrite constants
Browse files Browse the repository at this point in the history
  • Loading branch information
MESYETI committed Apr 15, 2024
1 parent 22dfb3e commit 93003f5
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 5 deletions.
11 changes: 11 additions & 0 deletions examples/random.cal
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
include "cores/select.cal"
include "std/io.cal"
include "std/random.cal"

let cell i
0 i !

while i @ 10 < do
rand printdec new_line
i @ 1 + i !
end
6 changes: 6 additions & 0 deletions examples/time.cal
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include "cores/select.cal"
include "std/io.cal"

requires Time

get_epoch_time printdec new_line
39 changes: 39 additions & 0 deletions micro_callisto.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
filetype: callisto

detect:
filename: "\\.(cal)$"

rules:
- statement: "\\b(func|end|begin|asm|include|inline|if|then|elseif|else|while|do)\\b"
- statement: "\\b(let|feature|implements|requires|struct|version|return|const)\\b"
- type: "\\b(addr|void|u8|i8|u16|i16|u32|i32|u64|i64|size|usize|cell|array)\\b"
- identifier: "\\$\\{?[0-9A-Za-z._!@#$*?-]+\\}?"
- identifier: "\\$\\{?[0-9A-Za-z._!@#$*?-]+\\}?"
- identifier: "\\*\\{?[0-9A-Za-z._!@#$*?-]+\\}?"
- identifier: "\\!\\{?[0-9A-Za-z._!@#$*?-]+\\}?"

- constant.string:
start: "\""
end: "\""
skip: "\\\\."
rules:
- constant.specialChar: "\\\\([\"'abfnrtv\\\\]|[0-3]?[0-7]{1,2}|x[0-9A-Fa-f]{1,2}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})"

- constant.string:
start: "'"
end: "'"
skip: "\\\\."
rules:
- constant.specialChar: "\\\\([\"'abfnrtv\\\\]|[0-3]?[0-7]{1,2}|x[0-9A-Fa-f]{1,2}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})"

- constant.number: "\\b([0-9.]*)\\b"
- constant.number: "\\b(0b[0-1.]*)\\b"
- constant.number: "\\b(0x[0-9A-Fa-f.]*)\\b"
- constant.number: "\\b(0o[0-7.]*)\\b"
- constant.bool: "\\b(true|false)\\b"

- comment:
start: "#"
end: "$"
rules:
- todo: "(TODO|XXX|FIXME):?"
8 changes: 6 additions & 2 deletions source/backends/linux86.d
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class BackendLinux86 : CompilerBackend {
NewConst("Array.sizeof", 8 * 3);
}

void NewConst(string name, long value, ErrorInfo error = ErrorInfo.init) {
override void NewConst(string name, long value, ErrorInfo error = ErrorInfo.init) {
consts[name] = Constant(new IntegerNode(error, value));
}

Expand Down Expand Up @@ -167,7 +167,7 @@ class BackendLinux86 : CompilerBackend {
output ~= "section .bss\n";

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

foreach (i, ref array ; arrays) {
Expand Down Expand Up @@ -522,6 +522,10 @@ class BackendLinux86 : CompilerBackend {
}

override void CompileConst(ConstNode node) {
if (node.name in consts) {
Error(node.error, "Constant '%s' already defined", node.name);
}

NewConst(node.name, node.value);
}
}
6 changes: 5 additions & 1 deletion source/backends/rm86.d
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class BackendRM86 : CompilerBackend {
NewConst("Array.sizeof", 2 * 3);
}

void NewConst(string name, long value, ErrorInfo error = ErrorInfo.init) {
override void NewConst(string name, long value, ErrorInfo error = ErrorInfo.init) {
consts[name] = Constant(new IntegerNode(error, value));
}

Expand Down Expand Up @@ -485,6 +485,10 @@ class BackendRM86 : CompilerBackend {
}

override void CompileConst(ConstNode node) {
if (node.name in consts) {
Error(node.error, "Constant '%s' already defined", node.name);
}

NewConst(node.name, node.value);
}
}
6 changes: 5 additions & 1 deletion source/backends/y16.d
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class BackendY16 : CompilerBackend {
NewConst("Array.elements", 4);
}

void NewConst(string name, long value, ErrorInfo error = ErrorInfo.init) {
override void NewConst(string name, long value, ErrorInfo error = ErrorInfo.init) {
consts[name] = Constant(new IntegerNode(error, value));
}

Expand Down Expand Up @@ -324,6 +324,10 @@ class BackendY16 : CompilerBackend {
}

override void CompileConst(ConstNode node) {
if (node.name in consts) {
Error(node.error, "Constant '%s' already defined", node.name);
}

NewConst(node.name, node.value);
}
}
1 change: 1 addition & 0 deletions source/compiler.d
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CompilerBackend {

abstract string[] GetVersions();
abstract string[] FinalCommands();
abstract void NewConst(string name, long value, ErrorInfo error);

abstract void Init();
abstract void End();
Expand Down
2 changes: 1 addition & 1 deletion std
Submodule std updated 2 files
+10 −0 cores/linux86.cal
+15 −0 std/random.cal

0 comments on commit 93003f5

Please sign in to comment.