Skip to content

Commit 93003f5

Browse files
committed
make const expressions unable to overwrite constants
1 parent 22dfb3e commit 93003f5

File tree

8 files changed

+74
-5
lines changed

8 files changed

+74
-5
lines changed

examples/random.cal

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
include "cores/select.cal"
2+
include "std/io.cal"
3+
include "std/random.cal"
4+
5+
let cell i
6+
0 i !
7+
8+
while i @ 10 < do
9+
rand printdec new_line
10+
i @ 1 + i !
11+
end

examples/time.cal

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
include "cores/select.cal"
2+
include "std/io.cal"
3+
4+
requires Time
5+
6+
get_epoch_time printdec new_line

micro_callisto.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
filetype: callisto
2+
3+
detect:
4+
filename: "\\.(cal)$"
5+
6+
rules:
7+
- statement: "\\b(func|end|begin|asm|include|inline|if|then|elseif|else|while|do)\\b"
8+
- statement: "\\b(let|feature|implements|requires|struct|version|return|const)\\b"
9+
- type: "\\b(addr|void|u8|i8|u16|i16|u32|i32|u64|i64|size|usize|cell|array)\\b"
10+
- identifier: "\\$\\{?[0-9A-Za-z._!@#$*?-]+\\}?"
11+
- identifier: "\\$\\{?[0-9A-Za-z._!@#$*?-]+\\}?"
12+
- identifier: "\\*\\{?[0-9A-Za-z._!@#$*?-]+\\}?"
13+
- identifier: "\\!\\{?[0-9A-Za-z._!@#$*?-]+\\}?"
14+
15+
- constant.string:
16+
start: "\""
17+
end: "\""
18+
skip: "\\\\."
19+
rules:
20+
- 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})"
21+
22+
- constant.string:
23+
start: "'"
24+
end: "'"
25+
skip: "\\\\."
26+
rules:
27+
- 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})"
28+
29+
- constant.number: "\\b([0-9.]*)\\b"
30+
- constant.number: "\\b(0b[0-1.]*)\\b"
31+
- constant.number: "\\b(0x[0-9A-Fa-f.]*)\\b"
32+
- constant.number: "\\b(0o[0-7.]*)\\b"
33+
- constant.bool: "\\b(true|false)\\b"
34+
35+
- comment:
36+
start: "#"
37+
end: "$"
38+
rules:
39+
- todo: "(TODO|XXX|FIXME):?"

source/backends/linux86.d

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class BackendLinux86 : CompilerBackend {
9292
NewConst("Array.sizeof", 8 * 3);
9393
}
9494

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

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

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

173173
foreach (i, ref array ; arrays) {
@@ -522,6 +522,10 @@ class BackendLinux86 : CompilerBackend {
522522
}
523523

524524
override void CompileConst(ConstNode node) {
525+
if (node.name in consts) {
526+
Error(node.error, "Constant '%s' already defined", node.name);
527+
}
528+
525529
NewConst(node.name, node.value);
526530
}
527531
}

source/backends/rm86.d

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class BackendRM86 : CompilerBackend {
8787
NewConst("Array.sizeof", 2 * 3);
8888
}
8989

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

@@ -485,6 +485,10 @@ class BackendRM86 : CompilerBackend {
485485
}
486486

487487
override void CompileConst(ConstNode node) {
488+
if (node.name in consts) {
489+
Error(node.error, "Constant '%s' already defined", node.name);
490+
}
491+
488492
NewConst(node.name, node.value);
489493
}
490494
}

source/backends/y16.d

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class BackendY16 : CompilerBackend {
8181
NewConst("Array.elements", 4);
8282
}
8383

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

@@ -324,6 +324,10 @@ class BackendY16 : CompilerBackend {
324324
}
325325

326326
override void CompileConst(ConstNode node) {
327+
if (node.name in consts) {
328+
Error(node.error, "Constant '%s' already defined", node.name);
329+
}
330+
327331
NewConst(node.name, node.value);
328332
}
329333
}

source/compiler.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class CompilerBackend {
1919

2020
abstract string[] GetVersions();
2121
abstract string[] FinalCommands();
22+
abstract void NewConst(string name, long value, ErrorInfo error);
2223

2324
abstract void Init();
2425
abstract void End();

std

0 commit comments

Comments
 (0)