Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
2b26d0d278
|
|||
|
236b50c813
|
|||
|
342b1ab107
|
|||
| 0732f68bcf | |||
|
0ac010ef8f
|
|||
|
c2208fbb15
|
|||
| 295f062797 |
18
Changelog.md
18
Changelog.md
@@ -1,5 +1,23 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
[0.2.3]
|
||||||
|
|
||||||
|
- Fixed stack underflow with function invocations
|
||||||
|
- They are still "heavy", but they should work as expected now
|
||||||
|
- Fixed issue where syscall functions were not allowed as infix operators
|
||||||
|
|
||||||
|
[0.2.2]
|
||||||
|
|
||||||
|
- Fixed some formatting issues when converting Markdown to Text Mesh Pro for
|
||||||
|
Stationpedia
|
||||||
|
- Added support for ternary expressions
|
||||||
|
- `let i = someValue ? 4 : 5;`
|
||||||
|
- `i = someValue ? 4 : 5;`
|
||||||
|
- This greedily evaluates both sides, so side effects like calling functions
|
||||||
|
is not recommended i.e.
|
||||||
|
- `i = someValue : doSomething() : doSomethingElse();`
|
||||||
|
- Both sides will be evaluated before calling the `select` instruction
|
||||||
|
|
||||||
[0.2.1]
|
[0.2.1]
|
||||||
|
|
||||||
- Added support for `loadSlot` and `setSlot`
|
- Added support for `loadSlot` and `setSlot`
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<ModMetadata xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
<ModMetadata xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
<Name>Slang</Name>
|
<Name>Slang</Name>
|
||||||
<Author>JoeDiertay</Author>
|
<Author>JoeDiertay</Author>
|
||||||
<Version>0.2.1</Version>
|
<Version>0.2.3</Version>
|
||||||
<Description>
|
<Description>
|
||||||
[h1]Slang: High-Level Programming for Stationeers[/h1]
|
[h1]Slang: High-Level Programming for Stationeers[/h1]
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ public static class SlangPatches
|
|||||||
{
|
{
|
||||||
private static ProgrammableChipMotherboard? _currentlyEditingMotherboard;
|
private static ProgrammableChipMotherboard? _currentlyEditingMotherboard;
|
||||||
private static AsciiString? _motherboardCachedCode;
|
private static AsciiString? _motherboardCachedCode;
|
||||||
|
private static Guid? _currentlyEditingGuid;
|
||||||
|
|
||||||
[HarmonyPatch(
|
[HarmonyPatch(
|
||||||
typeof(ProgrammableChipMotherboard),
|
typeof(ProgrammableChipMotherboard),
|
||||||
@@ -32,11 +33,13 @@ public static class SlangPatches
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var thisRef = Guid.NewGuid();
|
var thisRef = _currentlyEditingGuid ?? Guid.NewGuid();
|
||||||
|
|
||||||
// Ensure we cache this compiled code for later retreival.
|
// Ensure we cache this compiled code for later retreival.
|
||||||
GlobalCode.SetSource(thisRef, result);
|
GlobalCode.SetSource(thisRef, result);
|
||||||
|
|
||||||
|
_currentlyEditingGuid = null;
|
||||||
|
|
||||||
// Append REF to the bottom
|
// Append REF to the bottom
|
||||||
compiled += $"\n{GlobalCode.SLANG_REF}{thisRef}";
|
compiled += $"\n{GlobalCode.SLANG_REF}{thisRef}";
|
||||||
result = compiled;
|
result = compiled;
|
||||||
@@ -77,6 +80,7 @@ public static class SlangPatches
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_currentlyEditingGuid = sourceRef;
|
||||||
var slangSource = GlobalCode.GetSource(sourceRef);
|
var slangSource = GlobalCode.GetSource(sourceRef);
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(slangSource))
|
if (string.IsNullOrEmpty(slangSource))
|
||||||
@@ -223,6 +227,7 @@ public static class SlangPatches
|
|||||||
|
|
||||||
_currentlyEditingMotherboard = null;
|
_currentlyEditingMotherboard = null;
|
||||||
_motherboardCachedCode = null;
|
_motherboardCachedCode = null;
|
||||||
|
_currentlyEditingGuid = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HarmonyPatch(typeof(Stationpedia), nameof(Stationpedia.Regenerate))]
|
[HarmonyPatch(typeof(Stationpedia), nameof(Stationpedia.Regenerate))]
|
||||||
|
|||||||
@@ -26,12 +26,18 @@ public static class TextMeshProFormatter
|
|||||||
RegexOptions.Singleline
|
RegexOptions.Singleline
|
||||||
);
|
);
|
||||||
|
|
||||||
// 3. Handle Headers (## Header)
|
|
||||||
// Convert ## Header to large bold text
|
|
||||||
text = Regex.Replace(
|
text = Regex.Replace(
|
||||||
text,
|
text,
|
||||||
@"^##(\s+)?(.+)$",
|
@"^\s*##\s+(.+)$",
|
||||||
"<size=120%><b>$1</b></size>",
|
"<size=110%><color=#ffffff><b>$1</b></color></size>",
|
||||||
|
RegexOptions.Multiline
|
||||||
|
);
|
||||||
|
|
||||||
|
// 3. Handle # Headers SECOND (General)
|
||||||
|
text = Regex.Replace(
|
||||||
|
text,
|
||||||
|
@"^\s*#\s+(.+)$",
|
||||||
|
"<size=120%><color=#ffffff><b>$1</b></color></size>",
|
||||||
RegexOptions.Multiline
|
RegexOptions.Multiline
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<AssemblyName>StationeersSlang</AssemblyName>
|
<AssemblyName>StationeersSlang</AssemblyName>
|
||||||
<Description>Slang Compiler Bridge</Description>
|
<Description>Slang Compiler Bridge</Description>
|
||||||
<Version>0.2.1</Version>
|
<Version>0.2.3</Version>
|
||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
<LangVersion>latest</LangVersion>
|
<LangVersion>latest</LangVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|||||||
2
rust_compiler/Cargo.lock
generated
2
rust_compiler/Cargo.lock
generated
@@ -909,7 +909,7 @@ checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "slang"
|
name = "slang"
|
||||||
version = "0.2.1"
|
version = "0.2.3"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"clap",
|
"clap",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "slang"
|
name = "slang"
|
||||||
version = "0.2.1"
|
version = "0.2.3"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
use crate::compile;
|
use crate::compile;
|
||||||
|
use anyhow::Result;
|
||||||
use indoc::indoc;
|
use indoc::indoc;
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn simple_binary_expression() -> anyhow::Result<()> {
|
fn simple_binary_expression() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
debug
|
||||||
"
|
"
|
||||||
@@ -26,7 +27,7 @@ fn simple_binary_expression() -> anyhow::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn nested_binary_expressions() -> anyhow::Result<()> {
|
fn nested_binary_expressions() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
debug
|
||||||
"
|
"
|
||||||
@@ -51,6 +52,8 @@ fn nested_binary_expressions() -> anyhow::Result<()> {
|
|||||||
add r1 r10 r9
|
add r1 r10 r9
|
||||||
mul r2 r1 r8
|
mul r2 r1 r8
|
||||||
move r15 r2
|
move r15 r2
|
||||||
|
j L1
|
||||||
|
L1:
|
||||||
sub r0 sp 1
|
sub r0 sp 1
|
||||||
get ra db r0
|
get ra db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
@@ -71,7 +74,7 @@ fn nested_binary_expressions() -> anyhow::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn stress_test_constant_folding() -> anyhow::Result<()> {
|
fn stress_test_constant_folding() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
debug
|
||||||
"
|
"
|
||||||
@@ -94,7 +97,7 @@ fn stress_test_constant_folding() -> anyhow::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_constant_folding_with_variables_mixed_in() -> anyhow::Result<()> {
|
fn test_constant_folding_with_variables_mixed_in() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
debug
|
||||||
r#"
|
r#"
|
||||||
@@ -120,3 +123,55 @@ fn test_constant_folding_with_variables_mixed_in() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ternary_expression() -> Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
debug
|
||||||
|
r#"
|
||||||
|
let i = 1 > 2 ? 15 : 20;
|
||||||
|
"#
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
sgt r1 1 2
|
||||||
|
select r2 r1 15 20
|
||||||
|
move r8 r2 #i
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ternary_expression_assignment() -> Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
debug
|
||||||
|
r#"
|
||||||
|
let i = 0;
|
||||||
|
i = 1 > 2 ? 15 : 20;
|
||||||
|
"#
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 0 #i
|
||||||
|
sgt r1 1 2
|
||||||
|
select r2 r1 15 20
|
||||||
|
move r8 r2 #i
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ fn no_arguments() -> anyhow::Result<()> {
|
|||||||
j main
|
j main
|
||||||
doSomething:
|
doSomething:
|
||||||
push ra
|
push ra
|
||||||
|
L1:
|
||||||
sub r0 sp 1
|
sub r0 sp 1
|
||||||
get ra db r0
|
get ra db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
@@ -34,14 +35,17 @@ fn no_arguments() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn let_var_args() -> anyhow::Result<()> {
|
fn let_var_args() -> anyhow::Result<()> {
|
||||||
// !IMPORTANT this needs to be stabilized as it currently incorrectly calculates sp offset at
|
|
||||||
// both ends of the cleanup lifecycle
|
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
debug
|
||||||
"
|
"
|
||||||
fn doSomething(arg1) {};
|
fn mul2(arg1) {
|
||||||
let arg1 = 123;
|
return arg1 * 2;
|
||||||
let i = doSomething(arg1);
|
};
|
||||||
|
loop {
|
||||||
|
let arg1 = 123;
|
||||||
|
let i = mul2(arg1);
|
||||||
|
i = i ** 2;
|
||||||
|
}
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -50,23 +54,31 @@ fn let_var_args() -> anyhow::Result<()> {
|
|||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
doSomething:
|
mul2:
|
||||||
pop r8 #arg1
|
pop r8 #arg1
|
||||||
push ra
|
push ra
|
||||||
|
mul r1 r8 2
|
||||||
|
move r15 r1
|
||||||
|
j L1
|
||||||
|
L1:
|
||||||
sub r0 sp 1
|
sub r0 sp 1
|
||||||
get ra db r0
|
get ra db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
j ra
|
j ra
|
||||||
main:
|
main:
|
||||||
|
L2:
|
||||||
move r8 123 #arg1
|
move r8 123 #arg1
|
||||||
push r8
|
push r8
|
||||||
push r8
|
push r8
|
||||||
jal doSomething
|
jal mul2
|
||||||
sub r0 sp 1
|
sub r0 sp 1
|
||||||
get r8 db r0
|
get r8 db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
move r9 r15 #i
|
move r9 r15 #i
|
||||||
sub sp sp 1
|
pow r1 r9 2
|
||||||
|
move r9 r1 #i
|
||||||
|
j L2
|
||||||
|
L3:
|
||||||
"
|
"
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -97,7 +109,9 @@ fn inline_literal_args() -> anyhow::Result<()> {
|
|||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
debug
|
||||||
"
|
"
|
||||||
fn doSomething(arg1, arg2) {};
|
fn doSomething(arg1, arg2) {
|
||||||
|
return 5;
|
||||||
|
};
|
||||||
let thisVariableShouldStayInPlace = 123;
|
let thisVariableShouldStayInPlace = 123;
|
||||||
let returnedValue = doSomething(12, 34);
|
let returnedValue = doSomething(12, 34);
|
||||||
"
|
"
|
||||||
@@ -112,6 +126,9 @@ fn inline_literal_args() -> anyhow::Result<()> {
|
|||||||
pop r8 #arg2
|
pop r8 #arg2
|
||||||
pop r9 #arg1
|
pop r9 #arg1
|
||||||
push ra
|
push ra
|
||||||
|
move r15 5 #returnValue
|
||||||
|
j L1
|
||||||
|
L1:
|
||||||
sub r0 sp 1
|
sub r0 sp 1
|
||||||
get ra db r0
|
get ra db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
@@ -126,7 +143,6 @@ fn inline_literal_args() -> anyhow::Result<()> {
|
|||||||
get r8 db r0
|
get r8 db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
move r9 r15 #returnedValue
|
move r9 r15 #returnedValue
|
||||||
sub sp sp 1
|
|
||||||
"
|
"
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -154,6 +170,7 @@ fn mixed_args() -> anyhow::Result<()> {
|
|||||||
pop r8 #arg2
|
pop r8 #arg2
|
||||||
pop r9 #arg1
|
pop r9 #arg1
|
||||||
push ra
|
push ra
|
||||||
|
L1:
|
||||||
sub r0 sp 1
|
sub r0 sp 1
|
||||||
get ra db r0
|
get ra db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
@@ -168,7 +185,6 @@ fn mixed_args() -> anyhow::Result<()> {
|
|||||||
get r8 db r0
|
get r8 db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
move r9 r15 #returnValue
|
move r9 r15 #returnValue
|
||||||
sub sp sp 1
|
|
||||||
"
|
"
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -198,6 +214,8 @@ fn with_return_statement() -> anyhow::Result<()> {
|
|||||||
pop r8 #arg1
|
pop r8 #arg1
|
||||||
push ra
|
push ra
|
||||||
move r15 456 #returnValue
|
move r15 456 #returnValue
|
||||||
|
j L1
|
||||||
|
L1:
|
||||||
sub r0 sp 1
|
sub r0 sp 1
|
||||||
get ra db r0
|
get ra db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
@@ -233,6 +251,7 @@ fn with_negative_return_literal() -> anyhow::Result<()> {
|
|||||||
doSomething:
|
doSomething:
|
||||||
push ra
|
push ra
|
||||||
move r15 -1 #returnValue
|
move r15 -1 #returnValue
|
||||||
|
L1:
|
||||||
sub r0 sp 1
|
sub r0 sp 1
|
||||||
get ra db r0
|
get ra db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
|
|||||||
@@ -133,6 +133,8 @@ fn test_boolean_return() -> anyhow::Result<()> {
|
|||||||
getTrue:
|
getTrue:
|
||||||
push ra
|
push ra
|
||||||
move r15 1 #returnValue
|
move r15 1 #returnValue
|
||||||
|
j L1
|
||||||
|
L1:
|
||||||
sub r0 sp 1
|
sub r0 sp 1
|
||||||
get ra db r0
|
get ra db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ fn test_function_declaration_with_spillover_params() -> anyhow::Result<()> {
|
|||||||
pop r13 #arg4
|
pop r13 #arg4
|
||||||
pop r14 #arg3
|
pop r14 #arg3
|
||||||
push ra
|
push ra
|
||||||
|
L1:
|
||||||
sub r0 sp 1
|
sub r0 sp 1
|
||||||
get ra db r0
|
get ra db r0
|
||||||
sub sp sp 3
|
sub sp sp 3
|
||||||
@@ -31,6 +32,48 @@ fn test_function_declaration_with_spillover_params() -> anyhow::Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_early_return() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile!(debug r#"
|
||||||
|
// This is a test function declaration with no body
|
||||||
|
fn doSomething() {
|
||||||
|
if (1 == 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let i = 1 + 2;
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
doSomething();
|
||||||
|
"#);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
doSomething:
|
||||||
|
push ra
|
||||||
|
seq r1 1 1
|
||||||
|
beq r1 0 L2
|
||||||
|
j L1
|
||||||
|
L2:
|
||||||
|
move r8 3 #i
|
||||||
|
j L1
|
||||||
|
L1:
|
||||||
|
sub r0 sp 1
|
||||||
|
get ra db r0
|
||||||
|
sub sp sp 1
|
||||||
|
j ra
|
||||||
|
main:
|
||||||
|
jal doSomething
|
||||||
|
move r1 r15 #__binary_temp_2
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_function_declaration_with_register_params() -> anyhow::Result<()> {
|
fn test_function_declaration_with_register_params() -> anyhow::Result<()> {
|
||||||
let compiled = compile!(debug r#"
|
let compiled = compile!(debug r#"
|
||||||
@@ -47,6 +90,7 @@ fn test_function_declaration_with_register_params() -> anyhow::Result<()> {
|
|||||||
pop r8 #arg2
|
pop r8 #arg2
|
||||||
pop r9 #arg1
|
pop r9 #arg1
|
||||||
push ra
|
push ra
|
||||||
|
L1:
|
||||||
sub r0 sp 1
|
sub r0 sp 1
|
||||||
get ra db r0
|
get ra db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use parser::{
|
|||||||
AssignmentExpression, BinaryExpression, BlockExpression, ConstDeclarationExpression,
|
AssignmentExpression, BinaryExpression, BlockExpression, ConstDeclarationExpression,
|
||||||
DeviceDeclarationExpression, Expression, FunctionExpression, IfExpression,
|
DeviceDeclarationExpression, Expression, FunctionExpression, IfExpression,
|
||||||
InvocationExpression, Literal, LiteralOr, LiteralOrVariable, LogicalExpression,
|
InvocationExpression, Literal, LiteralOr, LiteralOrVariable, LogicalExpression,
|
||||||
LoopExpression, MemberAccessExpression, Span, Spanned, WhileExpression,
|
LoopExpression, MemberAccessExpression, Span, Spanned, TernaryExpression, WhileExpression,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
@@ -145,6 +145,7 @@ pub struct CompilerConfig {
|
|||||||
pub debug: bool,
|
pub debug: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
struct CompilationResult<'a> {
|
struct CompilationResult<'a> {
|
||||||
location: VariableLocation<'a>,
|
location: VariableLocation<'a>,
|
||||||
/// If Some, this is the name of the temporary variable that holds the result.
|
/// If Some, this is the name of the temporary variable that holds the result.
|
||||||
@@ -164,6 +165,7 @@ pub struct Compiler<'a, 'w, W: std::io::Write> {
|
|||||||
temp_counter: usize,
|
temp_counter: usize,
|
||||||
label_counter: usize,
|
label_counter: usize,
|
||||||
loop_stack: Vec<(Cow<'a, str>, Cow<'a, str>)>, // Stores (start_label, end_label)
|
loop_stack: Vec<(Cow<'a, str>, Cow<'a, str>)>, // Stores (start_label, end_label)
|
||||||
|
current_return_label: Option<Cow<'a, str>>,
|
||||||
pub errors: Vec<Error<'a>>,
|
pub errors: Vec<Error<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,6 +187,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
|||||||
temp_counter: 0,
|
temp_counter: 0,
|
||||||
label_counter: 0,
|
label_counter: 0,
|
||||||
loop_stack: Vec::new(),
|
loop_stack: Vec::new(),
|
||||||
|
current_return_label: None,
|
||||||
errors: Vec::new(),
|
errors: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -313,6 +316,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
|||||||
self.expression_assignment(assign_expr.node, scope)?;
|
self.expression_assignment(assign_expr.node, scope)?;
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
Expression::Ternary(tern) => Ok(Some(self.expression_ternary(tern.node, scope)?)),
|
||||||
Expression::Invocation(expr_invoke) => {
|
Expression::Invocation(expr_invoke) => {
|
||||||
self.expression_function_invocation(expr_invoke, scope)?;
|
self.expression_function_invocation(expr_invoke, scope)?;
|
||||||
// Invocation returns result in r15 (RETURN_REGISTER).
|
// Invocation returns result in r15 (RETURN_REGISTER).
|
||||||
@@ -740,6 +744,23 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
|||||||
|
|
||||||
(var_loc, None)
|
(var_loc, None)
|
||||||
}
|
}
|
||||||
|
Expression::Ternary(ternary) => {
|
||||||
|
let res = self.expression_ternary(ternary.node, scope)?;
|
||||||
|
println!("{res:?}");
|
||||||
|
let var_loc = scope.add_variable(
|
||||||
|
name_str.clone(),
|
||||||
|
LocationRequest::Persist,
|
||||||
|
Some(name_span),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let res_register = self.resolve_register(&res.location)?;
|
||||||
|
self.emit_variable_assignment(name_str, &var_loc, res_register)?;
|
||||||
|
|
||||||
|
if let Some(name) = res.temp_name {
|
||||||
|
scope.free_temp(name, None)?;
|
||||||
|
}
|
||||||
|
(var_loc, None)
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
return Err(Error::Unknown(
|
return Err(Error::Unknown(
|
||||||
format!("`{name_str}` declaration of this type is not supported/implemented."),
|
format!("`{name_str}` declaration of this type is not supported/implemented."),
|
||||||
@@ -881,7 +902,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
|||||||
fn expression_function_invocation(
|
fn expression_function_invocation(
|
||||||
&mut self,
|
&mut self,
|
||||||
invoke_expr: Spanned<InvocationExpression<'a>>,
|
invoke_expr: Spanned<InvocationExpression<'a>>,
|
||||||
stack: &mut VariableScope<'a, '_>,
|
parent_scope: &mut VariableScope<'a, '_>,
|
||||||
) -> Result<(), Error<'a>> {
|
) -> Result<(), Error<'a>> {
|
||||||
let InvocationExpression { name, arguments } = invoke_expr.node;
|
let InvocationExpression { name, arguments } = invoke_expr.node;
|
||||||
|
|
||||||
@@ -907,9 +928,10 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
|||||||
// Best to skip generation of this call to prevent bad IC10
|
// Best to skip generation of this call to prevent bad IC10
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
let mut stack = VariableScope::scoped(parent_scope);
|
||||||
|
|
||||||
// backup all used registers to the stack
|
// backup all used registers to the stack
|
||||||
let active_registers = stack.registers().cloned().collect::<Vec<_>>();
|
let active_registers = stack.registers();
|
||||||
for register in &active_registers {
|
for register in &active_registers {
|
||||||
stack.add_variable(
|
stack.add_variable(
|
||||||
Cow::from(format!("temp_{register}")),
|
Cow::from(format!("temp_{register}")),
|
||||||
@@ -972,7 +994,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
|||||||
}
|
}
|
||||||
Expression::Binary(bin_expr) => {
|
Expression::Binary(bin_expr) => {
|
||||||
// Compile the binary expression to a temp register
|
// Compile the binary expression to a temp register
|
||||||
let result = self.expression_binary(bin_expr, stack)?;
|
let result = self.expression_binary(bin_expr, &mut stack)?;
|
||||||
let reg_str = self.resolve_register(&result.location)?;
|
let reg_str = self.resolve_register(&result.location)?;
|
||||||
self.write_output(format!("push {reg_str}"))?;
|
self.write_output(format!("push {reg_str}"))?;
|
||||||
if let Some(name) = result.temp_name {
|
if let Some(name) = result.temp_name {
|
||||||
@@ -981,7 +1003,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
|||||||
}
|
}
|
||||||
Expression::Logical(log_expr) => {
|
Expression::Logical(log_expr) => {
|
||||||
// Compile the logical expression to a temp register
|
// Compile the logical expression to a temp register
|
||||||
let result = self.expression_logical(log_expr, stack)?;
|
let result = self.expression_logical(log_expr, &mut stack)?;
|
||||||
let reg_str = self.resolve_register(&result.location)?;
|
let reg_str = self.resolve_register(&result.location)?;
|
||||||
self.write_output(format!("push {reg_str}"))?;
|
self.write_output(format!("push {reg_str}"))?;
|
||||||
if let Some(name) = result.temp_name {
|
if let Some(name) = result.temp_name {
|
||||||
@@ -1000,7 +1022,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
|||||||
end_line: 0,
|
end_line: 0,
|
||||||
}, // Dummy span
|
}, // Dummy span
|
||||||
},
|
},
|
||||||
stack,
|
&mut stack,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
if let Some(result) = result_opt {
|
if let Some(result) = result_opt {
|
||||||
@@ -1208,6 +1230,45 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn expression_ternary(
|
||||||
|
&mut self,
|
||||||
|
expr: TernaryExpression<'a>,
|
||||||
|
scope: &mut VariableScope<'a, '_>,
|
||||||
|
) -> Result<CompilationResult<'a>, Error<'a>> {
|
||||||
|
let TernaryExpression {
|
||||||
|
condition,
|
||||||
|
true_value,
|
||||||
|
false_value,
|
||||||
|
} = expr;
|
||||||
|
|
||||||
|
let (cond, cond_clean) = self.compile_operand(*condition, scope)?;
|
||||||
|
let (true_val, true_clean) = self.compile_operand(*true_value, scope)?;
|
||||||
|
let (false_val, false_clean) = self.compile_operand(*false_value, scope)?;
|
||||||
|
|
||||||
|
let result_name = self.next_temp_name();
|
||||||
|
let result_loc = scope.add_variable(result_name.clone(), LocationRequest::Temp, None)?;
|
||||||
|
let result_reg = self.resolve_register(&result_loc)?;
|
||||||
|
|
||||||
|
self.write_output(format!(
|
||||||
|
"select {} {} {} {}",
|
||||||
|
result_reg, cond, true_val, false_val
|
||||||
|
))?;
|
||||||
|
|
||||||
|
if let Some(clean) = cond_clean {
|
||||||
|
scope.free_temp(clean, None)?;
|
||||||
|
}
|
||||||
|
if let Some(clean) = true_clean {
|
||||||
|
scope.free_temp(clean, None)?;
|
||||||
|
}
|
||||||
|
if let Some(clean) = false_clean {
|
||||||
|
scope.free_temp(clean, None)?;
|
||||||
|
}
|
||||||
|
Ok(CompilationResult {
|
||||||
|
location: result_loc,
|
||||||
|
temp_name: Some(result_name),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// Helper to resolve a location to a register string (e.g., "r0").
|
/// Helper to resolve a location to a register string (e.g., "r0").
|
||||||
/// Note: This does not handle Stack locations automatically, as they require
|
/// Note: This does not handle Stack locations automatically, as they require
|
||||||
/// instruction emission to load. Use `compile_operand` for general handling.
|
/// instruction emission to load. Use `compile_operand` for general handling.
|
||||||
@@ -1501,31 +1562,33 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
|||||||
mut expr: BlockExpression<'a>,
|
mut expr: BlockExpression<'a>,
|
||||||
parent_scope: &'v mut VariableScope<'a, '_>,
|
parent_scope: &'v mut VariableScope<'a, '_>,
|
||||||
) -> Result<(), Error<'a>> {
|
) -> Result<(), Error<'a>> {
|
||||||
|
fn get_expression_priority<'a>(expr: &Spanned<Expression<'a>>) -> u32 {
|
||||||
|
match expr.node {
|
||||||
|
Expression::ConstDeclaration(_) => 0,
|
||||||
|
Expression::DeviceDeclaration(_) => 1,
|
||||||
|
Expression::Function(_) => 2,
|
||||||
|
_ => 3,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// First, sort the expressions to ensure functions are hoisted
|
// First, sort the expressions to ensure functions are hoisted
|
||||||
expr.0.sort_by(|a, b| {
|
expr.0.sort_by(|a, b| {
|
||||||
if matches!(
|
let a_cost = get_expression_priority(a);
|
||||||
b.node,
|
let b_cost = get_expression_priority(b);
|
||||||
Expression::Function(_) | Expression::ConstDeclaration(_)
|
|
||||||
) && matches!(
|
a_cost.cmp(&b_cost)
|
||||||
a.node,
|
|
||||||
Expression::Function(_) | Expression::ConstDeclaration(_)
|
|
||||||
) {
|
|
||||||
std::cmp::Ordering::Equal
|
|
||||||
} else if matches!(
|
|
||||||
a.node,
|
|
||||||
Expression::Function(_) | Expression::ConstDeclaration(_)
|
|
||||||
) {
|
|
||||||
std::cmp::Ordering::Less
|
|
||||||
} else {
|
|
||||||
std::cmp::Ordering::Greater
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut scope = VariableScope::scoped(parent_scope);
|
let mut scope = VariableScope::scoped(parent_scope);
|
||||||
|
|
||||||
for expr in expr.0 {
|
for expr in expr.0 {
|
||||||
if !self.declared_main
|
if !self.declared_main
|
||||||
&& !matches!(expr.node, Expression::Function(_))
|
&& !matches!(
|
||||||
|
expr.node,
|
||||||
|
Expression::Function(_)
|
||||||
|
| Expression::ConstDeclaration(_)
|
||||||
|
| Expression::DeviceDeclaration(_)
|
||||||
|
)
|
||||||
&& !parent_scope.has_parent()
|
&& !parent_scope.has_parent()
|
||||||
{
|
{
|
||||||
self.write_output("main:")?;
|
self.write_output("main:")?;
|
||||||
@@ -1534,7 +1597,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
|||||||
|
|
||||||
match expr.node {
|
match expr.node {
|
||||||
Expression::Return(ret_expr) => {
|
Expression::Return(ret_expr) => {
|
||||||
self.expression_return(*ret_expr, &mut scope)?;
|
self.expression_return(ret_expr, &mut scope)?;
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
// Swallow errors within expressions so block can continue
|
// Swallow errors within expressions so block can continue
|
||||||
@@ -1564,133 +1627,149 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
|||||||
/// Takes the result of the expression and stores it in VariableScope::RETURN_REGISTER
|
/// Takes the result of the expression and stores it in VariableScope::RETURN_REGISTER
|
||||||
fn expression_return(
|
fn expression_return(
|
||||||
&mut self,
|
&mut self,
|
||||||
expr: Spanned<Expression<'a>>,
|
expr: Option<Box<Spanned<Expression<'a>>>>,
|
||||||
scope: &mut VariableScope<'a, '_>,
|
scope: &mut VariableScope<'a, '_>,
|
||||||
) -> Result<VariableLocation<'a>, Error<'a>> {
|
) -> Result<VariableLocation<'a>, Error<'a>> {
|
||||||
if let Expression::Negation(neg_expr) = &expr.node
|
if let Some(expr) = expr {
|
||||||
&& let Expression::Literal(spanned_lit) = &neg_expr.node
|
if let Expression::Negation(neg_expr) = &expr.node
|
||||||
&& let Literal::Number(neg_num) = &spanned_lit.node
|
&& let Expression::Literal(spanned_lit) = &neg_expr.node
|
||||||
{
|
&& let Literal::Number(neg_num) = &spanned_lit.node
|
||||||
let loc = VariableLocation::Persistant(VariableScope::RETURN_REGISTER);
|
{
|
||||||
self.emit_variable_assignment(
|
let loc = VariableLocation::Persistant(VariableScope::RETURN_REGISTER);
|
||||||
Cow::from("returnValue"),
|
self.emit_variable_assignment(
|
||||||
&loc,
|
Cow::from("returnValue"),
|
||||||
Cow::from(format!("-{neg_num}")),
|
&loc,
|
||||||
)?;
|
Cow::from(format!("-{neg_num}")),
|
||||||
return Ok(loc);
|
|
||||||
};
|
|
||||||
|
|
||||||
match expr.node {
|
|
||||||
Expression::Variable(var_name) => {
|
|
||||||
match scope.get_location_of(&var_name.node, Some(var_name.span)) {
|
|
||||||
Ok(loc) => match loc {
|
|
||||||
VariableLocation::Temporary(reg) | VariableLocation::Persistant(reg) => {
|
|
||||||
self.write_output(format!(
|
|
||||||
"move r{} r{reg} {}",
|
|
||||||
VariableScope::RETURN_REGISTER,
|
|
||||||
debug!(self, "#returnValue")
|
|
||||||
))?;
|
|
||||||
}
|
|
||||||
VariableLocation::Constant(lit) => {
|
|
||||||
let str = extract_literal(lit, false)?;
|
|
||||||
self.write_output(format!(
|
|
||||||
"move r{} {str} {}",
|
|
||||||
VariableScope::RETURN_REGISTER,
|
|
||||||
debug!(self, "#returnValue")
|
|
||||||
))?
|
|
||||||
}
|
|
||||||
VariableLocation::Stack(offset) => {
|
|
||||||
self.write_output(format!(
|
|
||||||
"sub r{} sp {offset}",
|
|
||||||
VariableScope::TEMP_STACK_REGISTER
|
|
||||||
))?;
|
|
||||||
self.write_output(format!(
|
|
||||||
"get r{} db r{}",
|
|
||||||
VariableScope::RETURN_REGISTER,
|
|
||||||
VariableScope::TEMP_STACK_REGISTER
|
|
||||||
))?;
|
|
||||||
}
|
|
||||||
VariableLocation::Device(_) => {
|
|
||||||
return Err(Error::Unknown(
|
|
||||||
"You can not return a device from a function.".into(),
|
|
||||||
Some(var_name.span),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Err(_) => {
|
|
||||||
self.errors.push(Error::UnknownIdentifier(
|
|
||||||
var_name.node.clone(),
|
|
||||||
var_name.span,
|
|
||||||
));
|
|
||||||
// Proceed with dummy
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Expression::Literal(spanned_lit) => match spanned_lit.node {
|
|
||||||
Literal::Number(num) => {
|
|
||||||
self.emit_variable_assignment(
|
|
||||||
Cow::from("returnValue"),
|
|
||||||
&VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
|
||||||
Cow::from(num.to_string()),
|
|
||||||
)?;
|
|
||||||
}
|
|
||||||
Literal::Boolean(b) => {
|
|
||||||
let val = if b { "1" } else { "0" };
|
|
||||||
self.emit_variable_assignment(
|
|
||||||
Cow::from("returnValue"),
|
|
||||||
&VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
|
||||||
Cow::from(val.to_string()),
|
|
||||||
)?;
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
},
|
|
||||||
Expression::Binary(bin_expr) => {
|
|
||||||
let result = self.expression_binary(bin_expr, scope)?;
|
|
||||||
let result_reg = self.resolve_register(&result.location)?;
|
|
||||||
self.write_output(format!(
|
|
||||||
"move r{} {}",
|
|
||||||
VariableScope::RETURN_REGISTER,
|
|
||||||
result_reg
|
|
||||||
))?;
|
|
||||||
if let Some(name) = result.temp_name {
|
|
||||||
scope.free_temp(name, None)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Expression::Logical(log_expr) => {
|
|
||||||
let result = self.expression_logical(log_expr, scope)?;
|
|
||||||
let result_reg = self.resolve_register(&result.location)?;
|
|
||||||
self.write_output(format!(
|
|
||||||
"move r{} {}",
|
|
||||||
VariableScope::RETURN_REGISTER,
|
|
||||||
result_reg
|
|
||||||
))?;
|
|
||||||
if let Some(name) = result.temp_name {
|
|
||||||
scope.free_temp(name, None)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Expression::MemberAccess(access) => {
|
|
||||||
// Return result of member access
|
|
||||||
let res_opt = self.expression(
|
|
||||||
Spanned {
|
|
||||||
node: Expression::MemberAccess(access),
|
|
||||||
span: expr.span,
|
|
||||||
},
|
|
||||||
scope,
|
|
||||||
)?;
|
)?;
|
||||||
if let Some(res) = res_opt {
|
return Ok(loc);
|
||||||
let reg = self.resolve_register(&res.location)?;
|
};
|
||||||
self.write_output(format!("move r{} {}", VariableScope::RETURN_REGISTER, reg))?;
|
|
||||||
if let Some(temp) = res.temp_name {
|
match expr.node {
|
||||||
scope.free_temp(temp, None)?;
|
Expression::Variable(var_name) => {
|
||||||
|
match scope.get_location_of(&var_name.node, Some(var_name.span)) {
|
||||||
|
Ok(loc) => match loc {
|
||||||
|
VariableLocation::Temporary(reg)
|
||||||
|
| VariableLocation::Persistant(reg) => {
|
||||||
|
self.write_output(format!(
|
||||||
|
"move r{} r{reg} {}",
|
||||||
|
VariableScope::RETURN_REGISTER,
|
||||||
|
debug!(self, "#returnValue")
|
||||||
|
))?;
|
||||||
|
}
|
||||||
|
VariableLocation::Constant(lit) => {
|
||||||
|
let str = extract_literal(lit, false)?;
|
||||||
|
self.write_output(format!(
|
||||||
|
"move r{} {str} {}",
|
||||||
|
VariableScope::RETURN_REGISTER,
|
||||||
|
debug!(self, "#returnValue")
|
||||||
|
))?
|
||||||
|
}
|
||||||
|
VariableLocation::Stack(offset) => {
|
||||||
|
self.write_output(format!(
|
||||||
|
"sub r{} sp {offset}",
|
||||||
|
VariableScope::TEMP_STACK_REGISTER
|
||||||
|
))?;
|
||||||
|
self.write_output(format!(
|
||||||
|
"get r{} db r{}",
|
||||||
|
VariableScope::RETURN_REGISTER,
|
||||||
|
VariableScope::TEMP_STACK_REGISTER
|
||||||
|
))?;
|
||||||
|
}
|
||||||
|
VariableLocation::Device(_) => {
|
||||||
|
return Err(Error::Unknown(
|
||||||
|
"You can not return a device from a function.".into(),
|
||||||
|
Some(var_name.span),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(_) => {
|
||||||
|
self.errors.push(Error::UnknownIdentifier(
|
||||||
|
var_name.node.clone(),
|
||||||
|
var_name.span,
|
||||||
|
));
|
||||||
|
// Proceed with dummy
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Expression::Literal(spanned_lit) => match spanned_lit.node {
|
||||||
|
Literal::Number(num) => {
|
||||||
|
self.emit_variable_assignment(
|
||||||
|
Cow::from("returnValue"),
|
||||||
|
&VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
||||||
|
Cow::from(num.to_string()),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
Literal::Boolean(b) => {
|
||||||
|
let val = if b { "1" } else { "0" };
|
||||||
|
self.emit_variable_assignment(
|
||||||
|
Cow::from("returnValue"),
|
||||||
|
&VariableLocation::Persistant(VariableScope::RETURN_REGISTER),
|
||||||
|
Cow::from(val.to_string()),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
},
|
||||||
|
Expression::Binary(bin_expr) => {
|
||||||
|
let result = self.expression_binary(bin_expr, scope)?;
|
||||||
|
let result_reg = self.resolve_register(&result.location)?;
|
||||||
|
self.write_output(format!(
|
||||||
|
"move r{} {}",
|
||||||
|
VariableScope::RETURN_REGISTER,
|
||||||
|
result_reg
|
||||||
|
))?;
|
||||||
|
if let Some(name) = result.temp_name {
|
||||||
|
scope.free_temp(name, None)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Expression::Logical(log_expr) => {
|
||||||
|
let result = self.expression_logical(log_expr, scope)?;
|
||||||
|
let result_reg = self.resolve_register(&result.location)?;
|
||||||
|
self.write_output(format!(
|
||||||
|
"move r{} {}",
|
||||||
|
VariableScope::RETURN_REGISTER,
|
||||||
|
result_reg
|
||||||
|
))?;
|
||||||
|
if let Some(name) = result.temp_name {
|
||||||
|
scope.free_temp(name, None)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Expression::MemberAccess(access) => {
|
||||||
|
// Return result of member access
|
||||||
|
let res_opt = self.expression(
|
||||||
|
Spanned {
|
||||||
|
node: Expression::MemberAccess(access),
|
||||||
|
span: expr.span,
|
||||||
|
},
|
||||||
|
scope,
|
||||||
|
)?;
|
||||||
|
if let Some(res) = res_opt {
|
||||||
|
let reg = self.resolve_register(&res.location)?;
|
||||||
|
self.write_output(format!(
|
||||||
|
"move r{} {}",
|
||||||
|
VariableScope::RETURN_REGISTER,
|
||||||
|
reg
|
||||||
|
))?;
|
||||||
|
if let Some(temp) = res.temp_name {
|
||||||
|
scope.free_temp(temp, None)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return Err(Error::Unknown(
|
||||||
|
format!("Unsupported `return` statement: {:?}", expr),
|
||||||
|
None,
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
}
|
||||||
return Err(Error::Unknown(
|
|
||||||
format!("Unsupported `return` statement: {:?}", expr),
|
if let Some(label) = &self.current_return_label {
|
||||||
None,
|
self.write_output(format!("j {}", label))?;
|
||||||
));
|
} else {
|
||||||
}
|
return Err(Error::Unknown(
|
||||||
|
"Return statement used outside of function context.".into(),
|
||||||
|
None,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(VariableLocation::Persistant(VariableScope::RETURN_REGISTER))
|
Ok(VariableLocation::Persistant(VariableScope::RETURN_REGISTER))
|
||||||
@@ -2286,8 +2365,13 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.write_output("push ra")?;
|
self.write_output("push ra")?;
|
||||||
|
|
||||||
|
let return_label = self.next_label_name();
|
||||||
|
|
||||||
|
let prev_return_label = self.current_return_label.replace(return_label.clone());
|
||||||
|
|
||||||
block_scope.add_variable(
|
block_scope.add_variable(
|
||||||
Cow::from(format!("{}_ra", name.node)),
|
return_label.clone(),
|
||||||
LocationRequest::Stack,
|
LocationRequest::Stack,
|
||||||
Some(name.span),
|
Some(name.span),
|
||||||
)?;
|
)?;
|
||||||
@@ -2295,7 +2379,7 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
|||||||
for expr in body.0 {
|
for expr in body.0 {
|
||||||
match expr.node {
|
match expr.node {
|
||||||
Expression::Return(ret_expr) => {
|
Expression::Return(ret_expr) => {
|
||||||
self.expression_return(*ret_expr, &mut block_scope)?;
|
self.expression_return(ret_expr, &mut block_scope)?;
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
// Swallow internal errors
|
// Swallow internal errors
|
||||||
@@ -2314,11 +2398,13 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get the saved return address and save it back into `ra`
|
// Get the saved return address and save it back into `ra`
|
||||||
let ra_res =
|
let ra_res = block_scope.get_location_of(&return_label, Some(name.span));
|
||||||
block_scope.get_location_of(&Cow::from(format!("{}_ra", name.node)), Some(name.span));
|
|
||||||
|
|
||||||
let ra_stack_offset = match ra_res {
|
let ra_stack_offset = match ra_res {
|
||||||
Ok(VariableLocation::Stack(offset)) => offset,
|
Ok(VariableLocation::Stack(offset)) => {
|
||||||
|
block_scope.free_temp(return_label.clone(), None)?;
|
||||||
|
offset
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
// If we can't find RA, we can't return properly.
|
// If we can't find RA, we can't return properly.
|
||||||
// This usually implies a compiler bug or scope tracking error.
|
// This usually implies a compiler bug or scope tracking error.
|
||||||
@@ -2329,6 +2415,10 @@ impl<'a, 'w, W: std::io::Write> Compiler<'a, 'w, W> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
self.current_return_label = prev_return_label;
|
||||||
|
|
||||||
|
self.write_output(format!("{}:", return_label))?;
|
||||||
|
|
||||||
self.write_output(format!(
|
self.write_output(format!(
|
||||||
"sub r{0} sp {ra_stack_offset}",
|
"sub r{0} sp {ra_stack_offset}",
|
||||||
VariableScope::TEMP_STACK_REGISTER
|
VariableScope::TEMP_STACK_REGISTER
|
||||||
|
|||||||
@@ -94,19 +94,21 @@ impl<'a, 'b> VariableScope<'a, 'b> {
|
|||||||
pub const RETURN_REGISTER: u8 = 15;
|
pub const RETURN_REGISTER: u8 = 15;
|
||||||
pub const TEMP_STACK_REGISTER: u8 = 0;
|
pub const TEMP_STACK_REGISTER: u8 = 0;
|
||||||
|
|
||||||
pub fn registers(&self) -> impl Iterator<Item = &u8> {
|
pub fn registers(&self) -> Vec<u8> {
|
||||||
self.var_lookup_table
|
let mut used = Vec::new();
|
||||||
.values()
|
|
||||||
.filter(|val| {
|
for r in TEMP {
|
||||||
matches!(
|
if !self.temporary_vars.contains(&r) {
|
||||||
val,
|
used.push(r);
|
||||||
VariableLocation::Temporary(_) | VariableLocation::Persistant(_)
|
}
|
||||||
)
|
}
|
||||||
})
|
|
||||||
.map(|loc| match loc {
|
for r in PERSIST {
|
||||||
VariableLocation::Persistant(reg) | VariableLocation::Temporary(reg) => reg,
|
if !self.persistant_vars.contains(&r) {
|
||||||
_ => unreachable!(),
|
used.push(r);
|
||||||
})
|
}
|
||||||
|
}
|
||||||
|
used
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn scoped(parent: &'b VariableScope<'a, 'b>) -> Self {
|
pub fn scoped(parent: &'b VariableScope<'a, 'b>) -> Self {
|
||||||
|
|||||||
@@ -293,12 +293,12 @@ impl<'a> Parser<'a> {
|
|||||||
// Handle Infix operators (Binary, Logical, Assignment)
|
// Handle Infix operators (Binary, Logical, Assignment)
|
||||||
if self_matches_peek!(
|
if self_matches_peek!(
|
||||||
self,
|
self,
|
||||||
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign)
|
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign | Symbol::Question)
|
||||||
) {
|
) {
|
||||||
return Ok(Some(self.infix(lhs)?));
|
return Ok(Some(self.infix(lhs)?));
|
||||||
} else if self_matches_current!(
|
} else if self_matches_current!(
|
||||||
self,
|
self,
|
||||||
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign)
|
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign | Symbol::Question)
|
||||||
) {
|
) {
|
||||||
self.tokenizer.seek(SeekFrom::Current(-1))?;
|
self.tokenizer.seek(SeekFrom::Current(-1))?;
|
||||||
return Ok(Some(self.infix(lhs)?));
|
return Ok(Some(self.infix(lhs)?));
|
||||||
@@ -766,9 +766,11 @@ impl<'a> Parser<'a> {
|
|||||||
Expression::Binary(_)
|
Expression::Binary(_)
|
||||||
| Expression::Logical(_)
|
| Expression::Logical(_)
|
||||||
| Expression::Invocation(_)
|
| Expression::Invocation(_)
|
||||||
|
| Expression::Syscall(_)
|
||||||
| Expression::Priority(_)
|
| Expression::Priority(_)
|
||||||
| Expression::Literal(_)
|
| Expression::Literal(_)
|
||||||
| Expression::Variable(_)
|
| Expression::Variable(_)
|
||||||
|
| Expression::Ternary(_)
|
||||||
| Expression::Negation(_)
|
| Expression::Negation(_)
|
||||||
| Expression::MemberAccess(_)
|
| Expression::MemberAccess(_)
|
||||||
| Expression::MethodCall(_) => {}
|
| Expression::MethodCall(_) => {}
|
||||||
@@ -788,7 +790,7 @@ impl<'a> Parser<'a> {
|
|||||||
// Include Assign in the operator loop
|
// Include Assign in the operator loop
|
||||||
while token_matches!(
|
while token_matches!(
|
||||||
temp_token,
|
temp_token,
|
||||||
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign)
|
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign | Symbol::Question | Symbol::Colon)
|
||||||
) {
|
) {
|
||||||
let operator = match temp_token.token_type {
|
let operator = match temp_token.token_type {
|
||||||
TokenType::Symbol(s) => s,
|
TokenType::Symbol(s) => s,
|
||||||
@@ -1019,7 +1021,52 @@ impl<'a> Parser<'a> {
|
|||||||
}
|
}
|
||||||
operators.retain(|symbol| !matches!(symbol, Symbol::LogicalOr));
|
operators.retain(|symbol| !matches!(symbol, Symbol::LogicalOr));
|
||||||
|
|
||||||
// --- PRECEDENCE LEVEL 8: Assignment (=) ---
|
// -- PRECEDENCE LEVEL 8: Ternary (x ? 1 : 2)
|
||||||
|
for i in (0..operators.len()).rev() {
|
||||||
|
if matches!(operators[i], Symbol::Question) {
|
||||||
|
// Ensure next operator is a colon
|
||||||
|
if i + 1 >= operators.len() || !matches!(operators[i + 1], Symbol::Colon) {
|
||||||
|
return Err(Error::InvalidSyntax(
|
||||||
|
self.current_span(),
|
||||||
|
"Ternary operator '?' missing matching ':'".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let false_branch = expressions.remove(i + 2);
|
||||||
|
let true_branch = expressions.remove(i + 1);
|
||||||
|
let condition = expressions.remove(i);
|
||||||
|
|
||||||
|
let span = Span {
|
||||||
|
start_line: condition.span.start_line,
|
||||||
|
end_line: false_branch.span.end_line,
|
||||||
|
start_col: condition.span.start_col,
|
||||||
|
end_col: false_branch.span.end_col,
|
||||||
|
};
|
||||||
|
|
||||||
|
let ternary_node = Spanned {
|
||||||
|
span,
|
||||||
|
node: TernaryExpression {
|
||||||
|
condition: Box::new(condition),
|
||||||
|
true_value: Box::new(true_branch),
|
||||||
|
false_value: Box::new(false_branch),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
expressions.insert(
|
||||||
|
i,
|
||||||
|
Spanned {
|
||||||
|
node: Expression::Ternary(ternary_node),
|
||||||
|
span,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// Remove the `?` and the `:` from the operators list
|
||||||
|
operators.remove(i);
|
||||||
|
operators.remove(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- PRECEDENCE LEVEL 9: Assignment (=) ---
|
||||||
// Assignment is Right Associative: a = b = c => a = (b = c)
|
// Assignment is Right Associative: a = b = c => a = (b = c)
|
||||||
// We iterate Right to Left
|
// We iterate Right to Left
|
||||||
for (i, operator) in operators.iter().enumerate().rev() {
|
for (i, operator) in operators.iter().enumerate().rev() {
|
||||||
@@ -1179,18 +1226,34 @@ impl<'a> Parser<'a> {
|
|||||||
// Need to capture return span
|
// Need to capture return span
|
||||||
let ret_start_span = Self::token_to_span(¤t_token);
|
let ret_start_span = Self::token_to_span(¤t_token);
|
||||||
self.assign_next()?;
|
self.assign_next()?;
|
||||||
let expression = self.expression()?.ok_or(Error::UnexpectedEOF)?;
|
|
||||||
|
let expr = if token_matches!(
|
||||||
|
self.current_token.as_ref().ok_or(Error::UnexpectedEOF)?,
|
||||||
|
TokenType::Symbol(Symbol::Semicolon)
|
||||||
|
) {
|
||||||
|
// rewind 1 token so we can check for the semicolon at the bottom of this function.
|
||||||
|
self.tokenizer.seek(SeekFrom::Current(-1))?;
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(self.expression()?.ok_or(Error::UnexpectedEOF)?)
|
||||||
|
};
|
||||||
|
|
||||||
let ret_span = Span {
|
let ret_span = Span {
|
||||||
start_line: ret_start_span.start_line,
|
start_line: ret_start_span.start_line,
|
||||||
start_col: ret_start_span.start_col,
|
start_col: ret_start_span.start_col,
|
||||||
end_line: expression.span.end_line,
|
end_line: expr
|
||||||
end_col: expression.span.end_col,
|
.as_ref()
|
||||||
|
.map(|e| e.span.end_line)
|
||||||
|
.unwrap_or(ret_start_span.end_line),
|
||||||
|
end_col: expr
|
||||||
|
.as_ref()
|
||||||
|
.map(|e| e.span.end_col)
|
||||||
|
.unwrap_or(ret_start_span.end_col),
|
||||||
};
|
};
|
||||||
|
|
||||||
let return_expr = Spanned {
|
let return_expr = Spanned {
|
||||||
span: ret_span,
|
span: ret_span,
|
||||||
node: Expression::Return(boxed!(expression)),
|
node: Expression::Return(expr.map(Box::new)),
|
||||||
};
|
};
|
||||||
expressions.push(return_expr);
|
expressions.push(return_expr);
|
||||||
|
|
||||||
|
|||||||
@@ -160,3 +160,37 @@ fn test_negative_literal_const() -> Result<()> {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ternary_expression() -> Result<()> {
|
||||||
|
let expr = parser!(r#"let i = x ? 1 : 2;"#).parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("(let i = (x ? 1 : 2))", expr.to_string());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_complex_binary_with_ternary() -> Result<()> {
|
||||||
|
let expr = parser!("let i = (x ? 1 : 3) * 2;").parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("(let i = ((x ? 1 : 3) * 2))", expr.to_string());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_operator_prescedence_with_ternary() -> Result<()> {
|
||||||
|
let expr = parser!("let x = x ? 1 : 3 * 2;").parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("(let x = (x ? 1 : (3 * 2)))", expr.to_string());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_nested_ternary_right_associativity() -> Result<()> {
|
||||||
|
let expr = parser!("let i = a ? b : c ? d : e;").parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("(let i = (a ? b : (c ? d : e)))", expr.to_string());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
@@ -277,6 +277,23 @@ pub struct WhileExpression<'a> {
|
|||||||
pub body: BlockExpression<'a>,
|
pub body: BlockExpression<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub struct TernaryExpression<'a> {
|
||||||
|
pub condition: Box<Spanned<Expression<'a>>>,
|
||||||
|
pub true_value: Box<Spanned<Expression<'a>>>,
|
||||||
|
pub false_value: Box<Spanned<Expression<'a>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> std::fmt::Display for TernaryExpression<'a> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"({} ? {} : {})",
|
||||||
|
self.condition, self.true_value, self.false_value
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> std::fmt::Display for WhileExpression<'a> {
|
impl<'a> std::fmt::Display for WhileExpression<'a> {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(f, "(while {} {})", self.condition, self.body)
|
write!(f, "(while {} {})", self.condition, self.body)
|
||||||
@@ -364,8 +381,9 @@ pub enum Expression<'a> {
|
|||||||
MethodCall(Spanned<MethodCallExpression<'a>>),
|
MethodCall(Spanned<MethodCallExpression<'a>>),
|
||||||
Negation(Box<Spanned<Expression<'a>>>),
|
Negation(Box<Spanned<Expression<'a>>>),
|
||||||
Priority(Box<Spanned<Expression<'a>>>),
|
Priority(Box<Spanned<Expression<'a>>>),
|
||||||
Return(Box<Spanned<Expression<'a>>>),
|
Return(Option<Box<Spanned<Expression<'a>>>>),
|
||||||
Syscall(Spanned<SysCall<'a>>),
|
Syscall(Spanned<SysCall<'a>>),
|
||||||
|
Ternary(Spanned<TernaryExpression<'a>>),
|
||||||
Variable(Spanned<Cow<'a, str>>),
|
Variable(Spanned<Cow<'a, str>>),
|
||||||
While(Spanned<WhileExpression<'a>>),
|
While(Spanned<WhileExpression<'a>>),
|
||||||
}
|
}
|
||||||
@@ -391,8 +409,17 @@ impl<'a> std::fmt::Display for Expression<'a> {
|
|||||||
Expression::MethodCall(e) => write!(f, "{}", e),
|
Expression::MethodCall(e) => write!(f, "{}", e),
|
||||||
Expression::Negation(e) => write!(f, "(-{})", e),
|
Expression::Negation(e) => write!(f, "(-{})", e),
|
||||||
Expression::Priority(e) => write!(f, "({})", e),
|
Expression::Priority(e) => write!(f, "({})", e),
|
||||||
Expression::Return(e) => write!(f, "(return {})", e),
|
Expression::Return(e) => write!(
|
||||||
|
f,
|
||||||
|
"(return {})",
|
||||||
|
if let Some(e) = e {
|
||||||
|
e.to_string()
|
||||||
|
} else {
|
||||||
|
"".to_string()
|
||||||
|
}
|
||||||
|
),
|
||||||
Expression::Syscall(e) => write!(f, "{}", e),
|
Expression::Syscall(e) => write!(f, "{}", e),
|
||||||
|
Expression::Ternary(e) => write!(f, "{}", e),
|
||||||
Expression::Variable(id) => write!(f, "{}", id),
|
Expression::Variable(id) => write!(f, "{}", id),
|
||||||
Expression::While(e) => write!(f, "{}", e),
|
Expression::While(e) => write!(f, "{}", e),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -225,6 +225,7 @@ pub enum TokenType<'a> {
|
|||||||
#[token(".", symbol!(Dot))]
|
#[token(".", symbol!(Dot))]
|
||||||
#[token("^", symbol!(Caret))]
|
#[token("^", symbol!(Caret))]
|
||||||
#[token("%", symbol!(Percent))]
|
#[token("%", symbol!(Percent))]
|
||||||
|
#[token("?", symbol!(Question))]
|
||||||
#[token("==", symbol!(Equal))]
|
#[token("==", symbol!(Equal))]
|
||||||
#[token("!=", symbol!(NotEqual))]
|
#[token("!=", symbol!(NotEqual))]
|
||||||
#[token("&&", symbol!(LogicalAnd))]
|
#[token("&&", symbol!(LogicalAnd))]
|
||||||
@@ -535,6 +536,8 @@ pub enum Symbol {
|
|||||||
Caret,
|
Caret,
|
||||||
/// Represents the `%` symbol
|
/// Represents the `%` symbol
|
||||||
Percent,
|
Percent,
|
||||||
|
/// Represents the `?` symbol
|
||||||
|
Question,
|
||||||
|
|
||||||
// Double Character Symbols
|
// Double Character Symbols
|
||||||
/// Represents the `==` symbol
|
/// Represents the `==` symbol
|
||||||
@@ -601,6 +604,7 @@ impl std::fmt::Display for Symbol {
|
|||||||
Self::Asterisk => write!(f, "*"),
|
Self::Asterisk => write!(f, "*"),
|
||||||
Self::Slash => write!(f, "/"),
|
Self::Slash => write!(f, "/"),
|
||||||
Self::LessThan => write!(f, "<"),
|
Self::LessThan => write!(f, "<"),
|
||||||
|
Self::Question => write!(f, "?"),
|
||||||
Self::LessThanOrEqual => write!(f, "<="),
|
Self::LessThanOrEqual => write!(f, "<="),
|
||||||
Self::GreaterThan => write!(f, ">"),
|
Self::GreaterThan => write!(f, ">"),
|
||||||
Self::GreaterThanOrEqual => write!(f, ">="),
|
Self::GreaterThanOrEqual => write!(f, ">="),
|
||||||
|
|||||||
Reference in New Issue
Block a user