Compare commits
39 Commits
0.4.3
...
72e6981176
| Author | SHA1 | Date | |
|---|---|---|---|
|
72e6981176
|
|||
|
d83341d90b
|
|||
|
d297f1bd46
|
|||
|
90a2becbbb
|
|||
|
a53ea7fd13
|
|||
|
c1a8af6aa7
|
|||
|
8c8ae23a27
|
|||
|
04c205b31d
|
|||
|
c133dc3c80
|
|||
|
9d8a867e5f
|
|||
|
e2a45f0d05
|
|||
|
fc13c465c0
|
|||
|
1ce3162fc0
|
|||
|
3092e97d41
|
|||
|
8029fa82b0
|
|||
|
6d8a22459c
|
|||
|
20f0f4b9a1
|
|||
|
5a88befac9
|
|||
|
e94fc0f5de
|
|||
|
b51800eb77
|
|||
|
87951ab12f
|
|||
|
00b0d4df26
|
|||
| 6ca53e8959 | |||
|
8dfdad3f34
|
|||
| e272737ea2 | |||
|
f679601818
|
|||
| 3ca6f97db1 | |||
|
34817ee111
|
|||
| 9eef8a77b6 | |||
|
de31851153
|
|||
|
3543b87561
|
|||
|
effef64add
|
|||
| 794b27b8c6 | |||
|
27e8987831
|
|||
|
0fdceac22c
|
|||
|
85f8b136e1
|
|||
|
c91086157a
|
|||
| 6bee591484 | |||
|
c3c14cec23
|
235
.github/copilot-instructions.md
vendored
Normal file
235
.github/copilot-instructions.md
vendored
Normal file
@@ -0,0 +1,235 @@
|
|||||||
|
# Slang Language Compiler - AI Agent Instructions
|
||||||
|
|
||||||
|
## Project Overview
|
||||||
|
|
||||||
|
**Slang** is a high-level programming language that compiles to IC10 assembly for the game Stationeers. The compiler is a multi-stage Rust system with a C# BepInEx mod integration layer.
|
||||||
|
|
||||||
|
**Key Goal:** Reduce manual IC10 assembly writing by providing C-like syntax with automatic register allocation and device abstraction.
|
||||||
|
|
||||||
|
## Architecture Overview
|
||||||
|
|
||||||
|
### Compilation Pipeline
|
||||||
|
|
||||||
|
The compiler follows a strict 4-stage pipeline (in [rust_compiler/libs/compiler/src/v1.rs](rust_compiler/libs/compiler/src/v1.rs)):
|
||||||
|
|
||||||
|
1. **Tokenizer** (libs/tokenizer/src/lib.rs) - Lexical analysis using `logos` crate
|
||||||
|
|
||||||
|
- Converts source text into tokens
|
||||||
|
- Tracks line/span information for error reporting
|
||||||
|
- Supports temperature literals (c/f/k suffixes)
|
||||||
|
|
||||||
|
2. **Parser** (libs/parser/src/lib.rs) - AST construction
|
||||||
|
|
||||||
|
- Recursive descent parser producing `Expression` tree
|
||||||
|
- Validates syntax, handles device declarations, function definitions
|
||||||
|
- Output: `Expression` enum containing tree nodes
|
||||||
|
|
||||||
|
3. **Compiler (v1)** (libs/compiler/src/v1.rs) - Semantic analysis & code generation
|
||||||
|
|
||||||
|
- Variable scope management and register allocation via `VariableManager`
|
||||||
|
- Emits IL instructions to `il::Instructions`
|
||||||
|
- Error types use `lsp_types::Diagnostic` for editor integration
|
||||||
|
|
||||||
|
4. **Optimizer** (libs/optimizer/src/lib.rs) - Post-generation optimization
|
||||||
|
- Currently optimizes leaf functions
|
||||||
|
- Optional pass before final output
|
||||||
|
|
||||||
|
### Cross-Language Integration
|
||||||
|
|
||||||
|
- **Rust Library** (`slang.dll`/`.so`): Core compiler logic via `safer-ffi` C FFI bindings
|
||||||
|
- **C# Mod** (`StationeersSlang.dll`): BepInEx plugin integrating with game UI
|
||||||
|
- **Generated Headers** (via `generate-headers` binary): Auto-generated C# bindings from Rust
|
||||||
|
|
||||||
|
### Key Types & Data Flow
|
||||||
|
|
||||||
|
- `Expression` tree (parser) → `v1::Compiler` processes → `il::Instructions` output
|
||||||
|
- `InstructionNode` wraps IC10 assembly with optional source span for debugging
|
||||||
|
- `VariableManager` tracks scopes, tracks const/device/let distinctions
|
||||||
|
- `Operand` enum represents register/literal/device-property values
|
||||||
|
|
||||||
|
## Critical Workflows
|
||||||
|
|
||||||
|
### Building
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd rust_compiler
|
||||||
|
# Build for both Linux and Windows targets
|
||||||
|
cargo build --release --target=x86_64-unknown-linux-gnu
|
||||||
|
cargo build --release --target=x86_64-pc-windows-gnu
|
||||||
|
|
||||||
|
# Generate C# FFI headers (requires "headers" feature)
|
||||||
|
cargo run --features headers --bin generate-headers
|
||||||
|
|
||||||
|
# Full build (run from root)
|
||||||
|
./build.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
### Testing
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd rust_compiler
|
||||||
|
# Run all tests
|
||||||
|
cargo test --package compiler --lib
|
||||||
|
|
||||||
|
# Run specific test file
|
||||||
|
cargo test --package compiler --lib tuple_literals
|
||||||
|
|
||||||
|
# Run single test
|
||||||
|
cargo test --package compiler --lib -- test::tuple_literals::test::test_tuple_literal_size_mismatch --exact --nocapture
|
||||||
|
```
|
||||||
|
|
||||||
|
### Quick Compilation
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd rust_compiler
|
||||||
|
# Compile Slang code to IC10 using current compiler changes
|
||||||
|
echo 'let x = 5;' | cargo run --bin slang --
|
||||||
|
# Or from file
|
||||||
|
cargo run --bin slang -- input.slang -o output.ic10
|
||||||
|
# Optimize the output with -z flag
|
||||||
|
cargo run --bin slang -- input.slang -o output.ic10 -z
|
||||||
|
```
|
||||||
|
|
||||||
|
## Codebase Patterns
|
||||||
|
|
||||||
|
### Test Structure
|
||||||
|
|
||||||
|
Tests follow a macro pattern in [libs/compiler/src/test/mod.rs](rust_compiler/libs/compiler/src/test/mod.rs):
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[test]
|
||||||
|
fn test_name() -> Result<()> {
|
||||||
|
let output = compile!(debug "slang code here");
|
||||||
|
assert_eq!(
|
||||||
|
output,
|
||||||
|
indoc! {
|
||||||
|
"Expected IC10 output here"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- `compile!()` macro: full pipeline from source to IC10
|
||||||
|
- `compile!(result ...)` for error checking
|
||||||
|
- `compile!(debug ...)` for intermediate IR inspection
|
||||||
|
- Test files organize by feature: `binary_expression.rs`, `syscall.rs`, `tuple_literals.rs`, etc.
|
||||||
|
|
||||||
|
### Error Handling
|
||||||
|
|
||||||
|
All stages return custom Error types implementing `From<lsp_types::Diagnostic>`:
|
||||||
|
|
||||||
|
- `tokenizer::Error` - Lexical errors
|
||||||
|
- `parser::Error<'a>` - Syntax errors
|
||||||
|
- `compiler::Error<'a>` - Semantic errors (unknown identifier, type mismatch)
|
||||||
|
- Device assignment prevention: `DeviceAssignment` error if reassigning device const
|
||||||
|
|
||||||
|
### Variable Scope Management
|
||||||
|
|
||||||
|
[variable_manager.rs](rust_compiler/libs/compiler/src/variable_manager.rs) handles:
|
||||||
|
|
||||||
|
- Tracking const vs mutable (let) distinction
|
||||||
|
- Device declarations as special scope items
|
||||||
|
- Function-local scopes with parameter handling
|
||||||
|
- Register allocation via `VariableLocation`
|
||||||
|
|
||||||
|
### LSP Integration
|
||||||
|
|
||||||
|
Error types implement conversion to `lsp_types::Diagnostic` for IDE feedback:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
impl<'a> From<Error<'a>> for lsp_types::Diagnostic { ... }
|
||||||
|
```
|
||||||
|
|
||||||
|
This enables real-time error reporting in the Stationeers IC10 Editor mod.
|
||||||
|
|
||||||
|
## Project-Specific Conventions
|
||||||
|
|
||||||
|
### Tuple Destructuring
|
||||||
|
|
||||||
|
The compiler supports tuple returns and multi-assignment:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let (x, y) = func(); // TupleDeclarationExpression
|
||||||
|
(x, y) = another_func(); // TupleAssignmentExpression
|
||||||
|
```
|
||||||
|
|
||||||
|
Compiler validates size matching with `TupleSizeMismatch` error.
|
||||||
|
|
||||||
|
### Device Property Access
|
||||||
|
|
||||||
|
Devices are first-class with property access:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
device ac = "d0";
|
||||||
|
ac.On = true;
|
||||||
|
ac.Temperature > 20c;
|
||||||
|
```
|
||||||
|
|
||||||
|
Parsed as `MemberAccessExpression`, compiled to device I/O syscalls.
|
||||||
|
|
||||||
|
### Temperature Literals
|
||||||
|
|
||||||
|
Unique language feature - automatic unit conversion at compile time:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
20c → 293.15k // Celsius to Kelvin
|
||||||
|
68f → 293.15k // Fahrenheit to Kelvin
|
||||||
|
```
|
||||||
|
|
||||||
|
Tokenizer produces `Literal::Number(Number(decimal, Some(Unit::Celsius)))`.
|
||||||
|
|
||||||
|
### Constants are Immutable
|
||||||
|
|
||||||
|
Once declared with `const`, reassignment is a compile error. Device assignment prevention is critical (prevents game logic bugs).
|
||||||
|
|
||||||
|
## Integration Points
|
||||||
|
|
||||||
|
### C# FFI (`csharp_mod/FfiGlue.cs`)
|
||||||
|
|
||||||
|
- Calls Rust compiler via marshaled FFI
|
||||||
|
- Passes source code, receives IC10 output
|
||||||
|
- Marshals errors as `Diagnostic` objects
|
||||||
|
|
||||||
|
### BepInEx Plugin Lifecycle
|
||||||
|
|
||||||
|
[csharp_mod/Plugin.cs](csharp_mod/Plugin.cs):
|
||||||
|
|
||||||
|
- Harmony patches for IC10 Editor integration
|
||||||
|
- Cleanup code for live-reload support (mod destruction)
|
||||||
|
- Logger integration for debug output
|
||||||
|
|
||||||
|
### CI/Build Target Matrix
|
||||||
|
|
||||||
|
- Linux: `x86_64-unknown-linux-gnu`
|
||||||
|
- Windows: `x86_64-pc-windows-gnu` (cross-compile from Linux)
|
||||||
|
- Both produce dynamic libraries + CLI binary
|
||||||
|
|
||||||
|
## Debugging Tips
|
||||||
|
|
||||||
|
1. **Print source spans:** `Span` type tracks line/column for error reporting
|
||||||
|
2. **IL inspection:** Use `compile!(debug source)` to view intermediate instructions
|
||||||
|
3. **Register allocation:** `VariableManager` logs scope changes; check for conflicts
|
||||||
|
4. **Syscall validation:** [parser/src/sys_call.rs](rust_compiler/libs/parser/src/sys_call.rs) lists all valid syscalls
|
||||||
|
5. **Tokenizer issues:** Check [tokenizer/src/token.rs](rust_compiler/libs/tokenizer/src/token.rs) for supported keywords/symbols
|
||||||
|
|
||||||
|
## Key Files for Common Tasks
|
||||||
|
|
||||||
|
| Task | File |
|
||||||
|
| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
|
| Add language feature | [libs/parser/src/lib.rs](rust_compiler/libs/parser/src/lib.rs) + test in [libs/compiler/src/test/](rust_compiler/libs/compiler/src/test/) |
|
||||||
|
| Fix codegen bug | [libs/compiler/src/v1.rs](rust_compiler/libs/compiler/src/v1.rs) (~3500 lines) |
|
||||||
|
| Add syscall | [libs/parser/src/sys_call.rs](rust_compiler/libs/parser/src/sys_call.rs) |
|
||||||
|
| Optimize output | [libs/optimizer/src/lib.rs](rust_compiler/libs/optimizer/src/lib.rs) |
|
||||||
|
| Mod integration | [csharp_mod/](csharp_mod/) |
|
||||||
|
| Language docs | [docs/language-reference.md](docs/language-reference.md) |
|
||||||
|
|
||||||
|
## Dependencies to Know
|
||||||
|
|
||||||
|
- `logos` - Tokenizer with derive macros
|
||||||
|
- `rust_decimal` - Precise decimal arithmetic for temperature conversion
|
||||||
|
- `safer-ffi` - Safe C FFI between Rust and C#
|
||||||
|
- `lsp-types` - Standard for editor diagnostics
|
||||||
|
- `thiserror` - Error type derivation
|
||||||
|
- `clap` - CLI argument parsing
|
||||||
|
- `anyhow` - Error handling in main binary
|
||||||
31
Changelog.md
31
Changelog.md
@@ -1,5 +1,36 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
[0.5.0]
|
||||||
|
|
||||||
|
- Added support for tuple types
|
||||||
|
- Added support for tuple returns from functions
|
||||||
|
- Added support for ignoring tuple values
|
||||||
|
- Fixed various compiler bugs
|
||||||
|
|
||||||
|
[0.4.7]
|
||||||
|
|
||||||
|
- Added support for Windows CRLF endings
|
||||||
|
|
||||||
|
[0.4.6]
|
||||||
|
|
||||||
|
- Fixed bug in compiler where you were unable to assign a `const` value to
|
||||||
|
a `let` variable
|
||||||
|
|
||||||
|
[0.4.5]
|
||||||
|
|
||||||
|
- Fixed issue where after clicking "Cancel" on the IC10 Editor, the side-by-side
|
||||||
|
IC10 output would no longer update with highlighting or code updates.
|
||||||
|
- Added ability to live-reload the mod while developing using the `ScriptEngine`
|
||||||
|
mod from BepInEx
|
||||||
|
- This required adding in cleanup code to cleanup references to the Rust DLL
|
||||||
|
before destroying the mod instance.
|
||||||
|
- Added BepInEx debug logging. This will ONLY show if you have debug logs
|
||||||
|
enabled in the BepInEx configuration file.
|
||||||
|
|
||||||
|
[0.4.4]
|
||||||
|
|
||||||
|
- Added Stationpedia docs back after removing all harmony patches from the mod
|
||||||
|
|
||||||
[0.4.3]
|
[0.4.3]
|
||||||
|
|
||||||
- Removed references to the `Mod` class from SLP. This was the root of the multiplayer
|
- Removed references to the `Mod` class from SLP. This was the root of the multiplayer
|
||||||
|
|||||||
@@ -2,11 +2,11 @@
|
|||||||
<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.4.3</Version>
|
<Version>0.5.0</Version>
|
||||||
<Description>
|
<Description>
|
||||||
[h1]Slang: High-Level Programming for Stationeers[/h1]
|
[h1]Slang: High-Level Programming for Stationeers[/h1]
|
||||||
|
|
||||||
Stop writing assembly. Start writing code.
|
Iterate faster. Stop writing assembly. Start writing code.
|
||||||
|
|
||||||
Slang (Stationeers Language) brings modern programming to Stationeers. It allows you to write scripts using a familiar C-style syntax (variables, functions, if/else, loops) directly in the in-game editor. When you hit confirm, Slang compiles your code into IC10 instantly.
|
Slang (Stationeers Language) brings modern programming to Stationeers. It allows you to write scripts using a familiar C-style syntax (variables, functions, if/else, loops) directly in the in-game editor. When you hit confirm, Slang compiles your code into IC10 instantly.
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ Slang (Stationeers Language) brings modern programming to Stationeers. It allows
|
|||||||
[h2]Features[/h2]
|
[h2]Features[/h2]
|
||||||
[list]
|
[list]
|
||||||
[*] [b]In-Game Compilation:[/b] No external tools needed. Write Slang directly in the IC editor.
|
[*] [b]In-Game Compilation:[/b] No external tools needed. Write Slang directly in the IC editor.
|
||||||
[*] [b]No More Register Juggling:[/b] Define variables with let (e.g., let temp = 300). The compiler manages r0-r15 for you.
|
[*] [b]No More Register Juggling:[/b] Define variables with let (e.g., let temp = 200c). The compiler manages r0-r15 for you.
|
||||||
[*] [b]Control Flow:[/b] Write readable logic with if, else, while, loop, break, and continue.
|
[*] [b]Control Flow:[/b] Write readable logic with if, else, while, loop, break, and continue.
|
||||||
[*] [b]Functions:[/b] Create reusable code blocks with arguments.
|
[*] [b]Functions:[/b] Create reusable code blocks with arguments.
|
||||||
[*] [b]Smart Editor:[/b] Get real-time syntax highlighting and error checking (red text) as you type.
|
[*] [b]Smart Editor:[/b] Get real-time syntax highlighting and error checking (red text) as you type.
|
||||||
@@ -53,14 +53,13 @@ loop {
|
|||||||
[h2]Known Issues (Beta)[/h2]
|
[h2]Known Issues (Beta)[/h2]
|
||||||
[list]
|
[list]
|
||||||
[*] [b]Stack Access:[/b] Direct stack memory access is disabled to prevent conflicts with the compiler's internal memory management. A workaround is being planned.
|
[*] [b]Stack Access:[/b] Direct stack memory access is disabled to prevent conflicts with the compiler's internal memory management. A workaround is being planned.
|
||||||
[*] [b]Documentation:[/b] In-game tooltips for syscalls (like load, set) are WIP. Check the "Slang" entry in the Stationpedia (F1) for help.
|
[*] [b]Documentation:[/b] In-game tooltips for syscalls (like load, set) are WIP. Check the "Slang" entry in the Stationpedia (F1) for help, or checkout [url=https://github.com/dbidwell94/stationeers_lang/blob/master/docs/getting-started.md]The Docs[/url] for guides on how to get started.
|
||||||
[/list]
|
[/list]
|
||||||
|
|
||||||
[h2]Planned Features[/h2]
|
[h2]Planned Features[/h2]
|
||||||
[list]
|
[list]
|
||||||
[*] Enhanced LSP features (Autocomplete, Go to Definition).
|
[*] Enhanced LSP features (Autocomplete, Go to Definition).
|
||||||
[*] Full feature parity with all IC10 instructions.
|
[*] Full feature parity with all IC10 instructions.
|
||||||
[*] Tutorials and beginner script examples.
|
|
||||||
[/list]
|
[/list]
|
||||||
|
|
||||||
[h2]FAQ[/h2]
|
[h2]FAQ[/h2]
|
||||||
@@ -70,10 +69,18 @@ A: The Slang compiler is built in Rust for performance and reliability. It is co
|
|||||||
[b]Q: Is this compatible with my current save?[/b]
|
[b]Q: Is this compatible with my current save?[/b]
|
||||||
A: Yes! Slang does not modify any existing IC10 code, it is only a compiler. As a matter of fact: if you wish to stop using Slang at any time, your compiled IC10 will still exist on the chip. However: Slang adds a comment at the bottom of your compiled IC10 which is a GZIP and base64 encoded version of your Slang source code. This might break line limits with Slang not installed. If you wish to no longer use Slang, I recommend you remove this comment from the source code after uninstalling Slang.
|
A: Yes! Slang does not modify any existing IC10 code, it is only a compiler. As a matter of fact: if you wish to stop using Slang at any time, your compiled IC10 will still exist on the chip. However: Slang adds a comment at the bottom of your compiled IC10 which is a GZIP and base64 encoded version of your Slang source code. This might break line limits with Slang not installed. If you wish to no longer use Slang, I recommend you remove this comment from the source code after uninstalling Slang.
|
||||||
|
|
||||||
|
[b]Q: Does this modify the in-game scripting language[/b]
|
||||||
|
A: No! Slang compiles directly to IC10. Any valid Slang file will produce valid IC10. The goal of this mod is twofold:
|
||||||
|
[list]
|
||||||
|
[*] Allow experienced users to quickly iterate on their scripts to get back into base upgrades faster
|
||||||
|
[*] Allow newcomers who already know C-style languages (JS/C#/Java/Rust/C/etc) to see how it maps to IC10 so they can get to understand IC10 better
|
||||||
|
[/list]
|
||||||
|
|
||||||
[h2]Useful Links[/h2]
|
[h2]Useful Links[/h2]
|
||||||
[url=https://github.com/dbidwell94/stationeers_lang]Source Code on GitHub[/url]
|
[url=https://github.com/dbidwell94/stationeers_lang]Source Code on GitHub[/url]
|
||||||
[url=https://discord.gg/stationeers]Stationeers Official Discord[/url]
|
[url=https://discord.gg/stationeers]Stationeers Official Discord[/url]
|
||||||
[url=https://discord.gg/M4sCfYMacs]Stationeers Modding Discord[/url]
|
[url=https://discord.gg/M4sCfYMacs]Stationeers Modding Discord[/url]
|
||||||
|
[url=https://github.com/dbidwell94/stationeers_lang/blob/master/docs/getting-started.md]Getting Started Guide[/url]
|
||||||
</Description>
|
</Description>
|
||||||
<ChangeLog xsi:nil="true" />
|
<ChangeLog xsi:nil="true" />
|
||||||
<WorkshopHandle>3619985558</WorkshopHandle>
|
<WorkshopHandle>3619985558</WorkshopHandle>
|
||||||
@@ -116,5 +123,7 @@ A: Yes! Slang does not modify any existing IC10 code, it is only a compiler. As
|
|||||||
See: https://github.com/StationeersLaunchPad/StationeersLaunchPad
|
See: https://github.com/StationeersLaunchPad/StationeersLaunchPad
|
||||||
|
|
||||||
Source Code: https://github.com/dbidwell94/stationeers_lang
|
Source Code: https://github.com/dbidwell94/stationeers_lang
|
||||||
|
|
||||||
|
Documentation: https://github.com/dbidwell94/stationeers_lang/blob/master/docs/getting-started.md
|
||||||
]]></InGameDescription>
|
]]></InGameDescription>
|
||||||
</ModMetadata>
|
</ModMetadata>
|
||||||
|
|||||||
61
README.md
61
README.md
@@ -1,30 +1,45 @@
|
|||||||
# Stationeers Language (slang)
|
# Slang Language Documentation
|
||||||
|
|
||||||
This is an ambitious attempt at creating:
|
Slang is a high-level programming language that compiles to IC10 assembly for [Stationeers](https://store.steampowered.com/app/544550/Stationeers/).
|
||||||
|
It provides a familiar C-like syntax while targeting the limited instruction set
|
||||||
|
of in-game IC10.
|
||||||
|
|
||||||
- A new programming language (slang)
|
## Quick Links
|
||||||
- A compiler to translate slang -> IC10
|
|
||||||
- A mod to allow direct input of slang in the in-game script editor to
|
|
||||||
automatically compile to IC10 before running
|
|
||||||
|
|
||||||
This project currently outputs 3 files:
|
- [Getting Started](docs/getting-started.md) - Installation and first program
|
||||||
|
- [Language Reference](docs/language-reference.md) - Complete syntax guide
|
||||||
|
- [Built-in Functions](docs/builtins.md) - System calls and math functions
|
||||||
|
- [Examples](docs/examples.md) - Real-world code samples
|
||||||
|
|
||||||
- A Linux CLI
|
## Overview
|
||||||
- A Windows CLI
|
|
||||||
- A Windows FFI dll
|
|
||||||
- Contains a single function: `compile_from_string`
|
|
||||||
|
|
||||||
The aim of this project is to lower the amount of time it takes to code simple
|
Slang aims to reduce the time spent writing IC10 assembly by providing:
|
||||||
scripts in Stationeers so you can get back to engineering atmospherics or
|
|
||||||
whatever you are working on. This project is NOT meant to fully replace IC10.
|
|
||||||
Obviously hand-coded assembly written by an experienced programmer is more
|
|
||||||
optimized and smaller than something that a C compiler will spit out. This is
|
|
||||||
the same way. It WILL produce valid IC10, but for large complicated projects it
|
|
||||||
might produce over the allowed limit of lines the in-game editor supports.
|
|
||||||
|
|
||||||
Current Unknowns
|
- **Familiar syntax** - C-like declarations, control flow, and expressions
|
||||||
|
- **Device abstraction** - Named device bindings with property access
|
||||||
|
- **Automatic register allocation** - No manual register management
|
||||||
|
- **Built-in functions** - Math operations and device I/O as function calls
|
||||||
|
- **Temperature literals** - Native support for Celsius, Fahrenheit, and Kelvin
|
||||||
|
|
||||||
- Should I support a configurable script line length in-game to allow larger
|
## Example
|
||||||
scripts to be saved?
|
|
||||||
- Should compilation be "behind the scenes" (in game editor will ALWAYS be what
|
```rust
|
||||||
you put in. IC10 will be IC10, slang will be slang)
|
device gasSensor = "d0";
|
||||||
|
device airCon = "d1";
|
||||||
|
|
||||||
|
const TARGET_TEMP = 20c;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
yield();
|
||||||
|
airCon.On = gasSensor.Temperature > TARGET_TEMP;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
This compiles to IC10 that monitors temperature and controls an air
|
||||||
|
conditioner.
|
||||||
|
|
||||||
|
## Project Status
|
||||||
|
|
||||||
|
Slang is under active development. It may produce suboptimal code for complex programs.
|
||||||
|
It is not a replacement for IC10, for performance-critical or large scripts,
|
||||||
|
hand-written IC10 may still be preferred.
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ using System.Linq;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using StationeersIC10Editor;
|
using StationeersIC10Editor;
|
||||||
using StationeersIC10Editor.IC10;
|
|
||||||
|
|
||||||
public class SlangFormatter : ICodeFormatter
|
public class SlangFormatter : ICodeFormatter
|
||||||
{
|
{
|
||||||
@@ -14,8 +13,25 @@ public class SlangFormatter : ICodeFormatter
|
|||||||
private CancellationTokenSource? _lspCancellationToken;
|
private CancellationTokenSource? _lspCancellationToken;
|
||||||
private object _tokenLock = new();
|
private object _tokenLock = new();
|
||||||
|
|
||||||
protected Editor? Ic10Editor = null;
|
protected Editor? __Ic10Editor = null;
|
||||||
private IC10CodeFormatter iC10CodeFormatter = new IC10CodeFormatter();
|
|
||||||
|
protected Editor Ic10Editor
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (__Ic10Editor == null)
|
||||||
|
{
|
||||||
|
var tab = Editor.ParentTab;
|
||||||
|
tab.ClearExtraEditors();
|
||||||
|
__Ic10Editor = new Editor(Editor.KeyHandler);
|
||||||
|
Ic10Editor.IsReadOnly = true;
|
||||||
|
tab.AddEditor(__Ic10Editor);
|
||||||
|
}
|
||||||
|
|
||||||
|
return __Ic10Editor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private string ic10CompilationResult = "";
|
private string ic10CompilationResult = "";
|
||||||
private List<SourceMapEntry> ic10SourceMap = new();
|
private List<SourceMapEntry> ic10SourceMap = new();
|
||||||
|
|
||||||
@@ -80,7 +96,7 @@ public class SlangFormatter : ICodeFormatter
|
|||||||
{
|
{
|
||||||
if (!Marshal.CompileFromString(RawText, out var compilationResult, out var sourceMap))
|
if (!Marshal.CompileFromString(RawText, out var compilationResult, out var sourceMap))
|
||||||
{
|
{
|
||||||
return "Compilation Error";
|
return "# Compilation Error";
|
||||||
}
|
}
|
||||||
|
|
||||||
return compilationResult + $"\n{EncodeSource(RawText, SLANG_SRC)}";
|
return compilationResult + $"\n{EncodeSource(RawText, SLANG_SRC)}";
|
||||||
@@ -118,6 +134,10 @@ public class SlangFormatter : ICodeFormatter
|
|||||||
return styledLine;
|
return styledLine;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This handles calling the `HandleLsp` function by creating a new `CancellationToken` and
|
||||||
|
/// cancelling the current call if applicable.
|
||||||
|
/// </summary>
|
||||||
private void HandleCodeChanged()
|
private void HandleCodeChanged()
|
||||||
{
|
{
|
||||||
CancellationToken token;
|
CancellationToken token;
|
||||||
@@ -133,6 +153,11 @@ public class SlangFormatter : ICodeFormatter
|
|||||||
_ = HandleLsp(inputSrc, token);
|
_ = HandleLsp(inputSrc, token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Takes a copy of the current source code and sends it to the Rust compiler in a background thread
|
||||||
|
/// to get diagnostic data. This also handles getting a compilation response of optimized IC10 for the
|
||||||
|
/// side-by-side IC10Editor to show with sourcemap highlighting.
|
||||||
|
/// </summary>
|
||||||
private async Task HandleLsp(string inputSrc, CancellationToken cancellationToken)
|
private async Task HandleLsp(string inputSrc, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -165,8 +190,7 @@ public class SlangFormatter : ICodeFormatter
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var (compilationSuccess, compiled, sourceMap) = await Task.Run(
|
var (compilationSuccess, compiled, sourceMap) = await Task.Run(() =>
|
||||||
() =>
|
|
||||||
{
|
{
|
||||||
var successful = Marshal.CompileFromString(
|
var successful = Marshal.CompileFromString(
|
||||||
inputSrc,
|
inputSrc,
|
||||||
@@ -174,15 +198,13 @@ public class SlangFormatter : ICodeFormatter
|
|||||||
out var sourceMap
|
out var sourceMap
|
||||||
);
|
);
|
||||||
return (successful, compiled, sourceMap);
|
return (successful, compiled, sourceMap);
|
||||||
},
|
});
|
||||||
cancellationToken
|
|
||||||
);
|
|
||||||
|
|
||||||
if (compilationSuccess)
|
if (compilationSuccess)
|
||||||
{
|
{
|
||||||
ic10CompilationResult = compiled;
|
ic10CompilationResult = compiled;
|
||||||
ic10SourceMap = sourceMap;
|
ic10SourceMap = sourceMap;
|
||||||
UpdateIc10Formatter();
|
UpdateIc10Content(Ic10Editor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (OperationCanceledException) { }
|
catch (OperationCanceledException) { }
|
||||||
@@ -192,22 +214,24 @@ public class SlangFormatter : ICodeFormatter
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Updates the underlying code in the IC10 Editor, after which will call `UpdateIc10Formatter` to
|
||||||
|
/// update highlighting of relavent fields.
|
||||||
|
/// </summary>
|
||||||
|
private void UpdateIc10Content(Editor editor)
|
||||||
|
{
|
||||||
|
editor.ResetCode(ic10CompilationResult);
|
||||||
|
UpdateIc10Formatter();
|
||||||
|
}
|
||||||
|
|
||||||
|
// This runs on the main thread. This function ONLY updates the highlighting of the IC10 code.
|
||||||
|
// If you need to update the code in the editor itself, you should use `UpdateIc10Content`.
|
||||||
private void UpdateIc10Formatter()
|
private void UpdateIc10Formatter()
|
||||||
{
|
{
|
||||||
var tab = Editor.ParentTab;
|
// Bail if our backing field is null. We don't want to set the field in this function. It
|
||||||
if (Ic10Editor == null)
|
// runs way too much and we might not even have source code to use.
|
||||||
{
|
if (__Ic10Editor == null)
|
||||||
iC10CodeFormatter = new IC10CodeFormatter();
|
return;
|
||||||
Ic10Editor = new Editor(Editor.KeyHandler);
|
|
||||||
Ic10Editor.IsReadOnly = true;
|
|
||||||
iC10CodeFormatter.Editor = Ic10Editor;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tab.Editors.Count < 2)
|
|
||||||
{
|
|
||||||
tab.AddEditor(Ic10Editor);
|
|
||||||
}
|
|
||||||
|
|
||||||
var caretPos = Editor.CaretPos.Line;
|
var caretPos = Editor.CaretPos.Line;
|
||||||
|
|
||||||
// get the slang sourceMap at the current editor line
|
// get the slang sourceMap at the current editor line
|
||||||
@@ -215,10 +239,6 @@ public class SlangFormatter : ICodeFormatter
|
|||||||
entry.SlangSource.StartLine == caretPos || entry.SlangSource.EndLine == caretPos
|
entry.SlangSource.StartLine == caretPos || entry.SlangSource.EndLine == caretPos
|
||||||
);
|
);
|
||||||
|
|
||||||
// extract the current "context" of the ic10 compilation. The current Slang source line
|
|
||||||
// should be directly next to the compiled IC10 source line, and we should highlight the
|
|
||||||
// IC10 code that directly represents the Slang source
|
|
||||||
|
|
||||||
Ic10Editor.ResetCode(ic10CompilationResult);
|
Ic10Editor.ResetCode(ic10CompilationResult);
|
||||||
|
|
||||||
if (lines.Count() < 1)
|
if (lines.Count() < 1)
|
||||||
@@ -245,7 +265,11 @@ public class SlangFormatter : ICodeFormatter
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// This runs on the Main Thread
|
/// <summary>
|
||||||
|
/// Takes diagnostics from the Rust FFI compiler and applies it as semantic tokens to the
|
||||||
|
/// source in this editor.
|
||||||
|
/// This runs on the Main Thread
|
||||||
|
/// </summary>
|
||||||
private void ApplyDiagnostics(Dictionary<uint, IGrouping<uint, Diagnostic>> dict)
|
private void ApplyDiagnostics(Dictionary<uint, IGrouping<uint, Diagnostic>> dict)
|
||||||
{
|
{
|
||||||
HashSet<uint> linesToRefresh;
|
HashSet<uint> linesToRefresh;
|
||||||
|
|||||||
@@ -67,7 +67,9 @@ public static class Marshal
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
_libraryHandle = LoadLibrary(ExtractNativeLibrary(Ffi.RustLib));
|
_libraryHandle = LoadLibrary(ExtractNativeLibrary(Ffi.RustLib));
|
||||||
|
L.Debug("Rust DLL loaded successfully. Enjoy native speed compilations!");
|
||||||
CodeFormatters.RegisterFormatter("Slang", typeof(SlangFormatter), true);
|
CodeFormatters.RegisterFormatter("Slang", typeof(SlangFormatter), true);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -91,8 +93,13 @@ public static class Marshal
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
FreeLibrary(_libraryHandle);
|
CodeFormatters.RegisterFormatter("Slang", typeof(PlainTextFormatter), true);
|
||||||
|
if (!FreeLibrary(_libraryHandle))
|
||||||
|
{
|
||||||
|
L.Warning("Unable to free Rust library");
|
||||||
|
}
|
||||||
_libraryHandle = IntPtr.Zero;
|
_libraryHandle = IntPtr.Zero;
|
||||||
|
L.Debug("Rust DLL library freed");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -191,9 +198,9 @@ public static class Marshal
|
|||||||
|
|
||||||
Assembly assembly = Assembly.GetExecutingAssembly();
|
Assembly assembly = Assembly.GetExecutingAssembly();
|
||||||
|
|
||||||
using (Stream stream = assembly.GetManifestResourceStream(libName))
|
using (Stream resourceStream = assembly.GetManifestResourceStream(libName))
|
||||||
{
|
{
|
||||||
if (stream == null)
|
if (resourceStream == null)
|
||||||
{
|
{
|
||||||
L.Error(
|
L.Error(
|
||||||
$"{libName} not found. This means it was not embedded in the mod. Please contact the mod author!"
|
$"{libName} not found. This means it was not embedded in the mod. Please contact the mod author!"
|
||||||
@@ -201,18 +208,85 @@ public static class Marshal
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if file exists and contents are identical to avoid overwriting locked files
|
||||||
|
if (File.Exists(destinationPath))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (
|
||||||
|
FileStream fileStream = new FileStream(
|
||||||
|
destinationPath,
|
||||||
|
FileMode.Open,
|
||||||
|
FileAccess.Read,
|
||||||
|
FileShare.ReadWrite
|
||||||
|
)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (resourceStream.Length == fileStream.Length)
|
||||||
|
{
|
||||||
|
if (StreamsContentsAreEqual(resourceStream, fileStream))
|
||||||
|
{
|
||||||
|
L.Debug(
|
||||||
|
$"DLL {libName} already exists and matches. Skipping extraction."
|
||||||
|
);
|
||||||
|
return destinationPath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IOException ex)
|
||||||
|
{
|
||||||
|
L.Warning(
|
||||||
|
$"Could not verify existing {libName}, attempting overwrite. {ex.Message}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resourceStream.Position = 0;
|
||||||
|
|
||||||
|
// Attempt to overwrite if missing or different
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (FileStream fileStream = new FileStream(destinationPath, FileMode.Create))
|
using (FileStream fileStream = new FileStream(destinationPath, FileMode.Create))
|
||||||
{
|
{
|
||||||
stream.CopyTo(fileStream);
|
resourceStream.CopyTo(fileStream);
|
||||||
}
|
}
|
||||||
return destinationPath;
|
return destinationPath;
|
||||||
}
|
}
|
||||||
catch (IOException e)
|
catch (IOException e)
|
||||||
{
|
{
|
||||||
L.Warning($"Could not overwrite {libName} (it might be in use): {e.Message}");
|
// If we fail here, the file is likely locked.
|
||||||
return "";
|
// However, if we are here, it means the file is DIFFERENT or we couldn't read it.
|
||||||
|
// As a fallback for live-reload, we can try returning the path anyway
|
||||||
|
// assuming the existing locked file might still work.
|
||||||
|
L.Warning(
|
||||||
|
$"Could not overwrite {libName} (it might be in use): {e.Message}. Attempting to use existing file."
|
||||||
|
);
|
||||||
|
return destinationPath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool StreamsContentsAreEqual(Stream stream1, Stream stream2)
|
||||||
|
{
|
||||||
|
const int bufferSize = 4096;
|
||||||
|
byte[] buffer1 = new byte[bufferSize];
|
||||||
|
byte[] buffer2 = new byte[bufferSize];
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
int count1 = stream1.Read(buffer1, 0, bufferSize);
|
||||||
|
int count2 = stream2.Read(buffer2, 0, bufferSize);
|
||||||
|
|
||||||
|
if (count1 != count2)
|
||||||
|
return false;
|
||||||
|
if (count1 == 0)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
for (int i = 0; i < count1; i++)
|
||||||
|
{
|
||||||
|
if (buffer1[i] != buffer2[i])
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
18
csharp_mod/Patches.cs
Normal file
18
csharp_mod/Patches.cs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
namespace Slang;
|
||||||
|
|
||||||
|
using Assets.Scripts.UI;
|
||||||
|
using HarmonyLib;
|
||||||
|
|
||||||
|
[HarmonyPatch]
|
||||||
|
public static class SlangPatches
|
||||||
|
{
|
||||||
|
[HarmonyPatch(typeof(Stationpedia), nameof(Stationpedia.Regenerate))]
|
||||||
|
[HarmonyPostfix]
|
||||||
|
public static void Stationpedia_Regenerate()
|
||||||
|
{
|
||||||
|
foreach (var page in Marshal.GetSlangDocs())
|
||||||
|
{
|
||||||
|
Stationpedia.Register(page);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
using System.Text.RegularExpressions;
|
|
||||||
using BepInEx;
|
using BepInEx;
|
||||||
using HarmonyLib;
|
using HarmonyLib;
|
||||||
|
|
||||||
@@ -40,43 +39,32 @@ namespace Slang
|
|||||||
{
|
{
|
||||||
public const string PluginGuid = "com.biddydev.slang";
|
public const string PluginGuid = "com.biddydev.slang";
|
||||||
public const string PluginName = "Slang";
|
public const string PluginName = "Slang";
|
||||||
public const string PluginVersion = "0.4.3";
|
public const string PluginVersion = "0.5.0";
|
||||||
|
|
||||||
private Harmony? _harmony;
|
private static Harmony? _harmony;
|
||||||
|
|
||||||
private static Regex? _slangSourceCheck = null;
|
public void Awake()
|
||||||
|
|
||||||
private static Regex SlangSourceCheck
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
if (_slangSourceCheck is null)
|
|
||||||
{
|
|
||||||
_slangSourceCheck = new Regex(@"[;{}()]|\b(let|fn|device)\b|\/\/");
|
|
||||||
}
|
|
||||||
|
|
||||||
return _slangSourceCheck;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool IsSlangSource(ref string input)
|
|
||||||
{
|
|
||||||
return SlangSourceCheck.IsMatch(input);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Awake()
|
|
||||||
{
|
{
|
||||||
L.SetLogger(Logger);
|
L.SetLogger(Logger);
|
||||||
this._harmony = new Harmony(PluginGuid);
|
_harmony = new Harmony(PluginGuid);
|
||||||
|
|
||||||
// If we failed to load the compiler, bail from the rest of the patches. It won't matter,
|
// If we failed to load the compiler, bail from the rest of the patches. It won't matter,
|
||||||
// as the compiler itself has failed to load.
|
// as the compiler itself has failed to load.
|
||||||
if (!Marshal.Init())
|
if (!Marshal.Init())
|
||||||
{
|
{
|
||||||
|
L.Error("Marshal failed to init");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._harmony.PatchAll();
|
_harmony.PatchAll();
|
||||||
|
L.Debug("Ran Harmony patches");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnDestroy()
|
||||||
|
{
|
||||||
|
Marshal.Destroy();
|
||||||
|
_harmony?.UnpatchSelf();
|
||||||
|
L.Debug("Cleaned up Harmony patches");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.4.2</Version>
|
<Version>0.5.0</Version>
|
||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
<LangVersion>latest</LangVersion>
|
<LangVersion>latest</LangVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|||||||
302
docs/builtins.md
Normal file
302
docs/builtins.md
Normal file
@@ -0,0 +1,302 @@
|
|||||||
|
# Built-in Functions
|
||||||
|
|
||||||
|
<!--toc:start-->
|
||||||
|
|
||||||
|
- [Built-in Functions](#built-in-functions)
|
||||||
|
- [System Functions](#system-functions)
|
||||||
|
- [`yield()`](#yield)
|
||||||
|
- [`sleep(ticks)`](#sleepticks)
|
||||||
|
- [`hash(prefabName)`](#hashprefabname)
|
||||||
|
- [Device I/O Functions](#device-io-functions)
|
||||||
|
- [Reading from Devices](#reading-from-devices)
|
||||||
|
- [Load from device](#load-from-device)
|
||||||
|
- [Load From Device Batched](#load-from-device-batched)
|
||||||
|
- [Load From Device Batched Named](#load-from-device-batched-named)
|
||||||
|
- [Load Slot](#load-slot)
|
||||||
|
- [Load Reagent](#load-reagent)
|
||||||
|
- [Writing to Devices](#writing-to-devices)
|
||||||
|
- [Set On Device](#set-on-device)
|
||||||
|
- [Set On Device Batched](#set-on-device-batched)
|
||||||
|
- [Set On Device Batched Named](#set-on-device-batched-named)
|
||||||
|
- [Set Slot](#set-slot)
|
||||||
|
- [Math Functions](#math-functions)
|
||||||
|
- [Trigonometric Functions](#trigonometric-functions)
|
||||||
|
- [Trig Example](#trig-example)
|
||||||
|
- [Rounding Functions](#rounding-functions)
|
||||||
|
- [Rounding Example](#rounding-example)
|
||||||
|
- [Other Math Functions](#other-math-functions)
|
||||||
|
- [Math Example](#math-example)
|
||||||
|
- [See Also](#see-also)
|
||||||
|
<!--toc:end-->
|
||||||
|
|
||||||
|
Slang provides built-in functions for device I/O and mathematical operations.
|
||||||
|
These map directly to IC10 instructions.
|
||||||
|
|
||||||
|
## System Functions
|
||||||
|
|
||||||
|
### `yield()`
|
||||||
|
|
||||||
|
Pauses execution for exactly one game tick.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
yield();
|
||||||
|
```
|
||||||
|
|
||||||
|
**IC10:** `yield`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### `sleep(ticks)`
|
||||||
|
|
||||||
|
Pauses execution for the specified number of ticks.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
sleep(10); // Sleep for 10 ticks
|
||||||
|
```
|
||||||
|
|
||||||
|
**IC10:** `sleep ticks`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### `hash(prefabName)`
|
||||||
|
|
||||||
|
Computes the in-game hash for a prefab name. The hash is computed at compile
|
||||||
|
time and no runtime code is generated.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
const AC_HASH = hash("StructureAirConditioner");
|
||||||
|
```
|
||||||
|
|
||||||
|
**Note:** This is different from IC10's `hash` instruction, which computes the
|
||||||
|
hash at runtime.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
setBatched(AC_HASH, "On", 0);
|
||||||
|
```
|
||||||
|
|
||||||
|
**IC10:** `sb -2087593337 On 0` (no hash computation at runtime)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Device I/O Functions
|
||||||
|
|
||||||
|
### Reading from Devices
|
||||||
|
|
||||||
|
#### Load from device
|
||||||
|
|
||||||
|
`load(device, property)` / `l(device, property)`
|
||||||
|
|
||||||
|
Loads a property value from a device:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let temp = load(sensor, "Temperature");
|
||||||
|
let temp = l(sensor, "Temperature");
|
||||||
|
|
||||||
|
// Preferred: use dot notation
|
||||||
|
let temp = sensor.Temperature;
|
||||||
|
```
|
||||||
|
|
||||||
|
**IC10:** `l r? d? var`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### Load From Device Batched
|
||||||
|
|
||||||
|
`loadBatched(deviceHash, property, batchMode)` / `lb(...)`
|
||||||
|
|
||||||
|
Loads a property from all devices matching a hash, aggregated by batch mode:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
const SENSOR = hash("StructureGasSensor");
|
||||||
|
let avgTemp = loadBatched(SENSOR, "Temperature", "Average");
|
||||||
|
let maxTemp = lb(SENSOR, "Temperature", "Maximum");
|
||||||
|
```
|
||||||
|
|
||||||
|
**Batch Modes:** `"Average"`, `"Sum"`, `"Minimum"`, `"Maximum"`
|
||||||
|
|
||||||
|
**IC10:** `lb r? deviceHash logicType batchMode`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### Load From Device Batched Named
|
||||||
|
|
||||||
|
`loadBatchedNamed(deviceHash, nameHash, property, batchMode)` / `lbn(...)`
|
||||||
|
|
||||||
|
Loads a property from devices matching both device hash and name hash:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
const SENSOR_HASH = hash("StructureGasSensor");
|
||||||
|
const SENSOR_NAME_HASH = hash("Outdoor Gas Sensor");
|
||||||
|
let avgTemp = loadBatchedNamed(SENSOR_HASH, SENSOR_NAME_HASH, "Temperature", "Average");
|
||||||
|
let maxTemp = lbn(SENSOR_HASH, SENSOR_NAME_HASH, "Temperature", "Maximum");
|
||||||
|
```
|
||||||
|
|
||||||
|
**IC10:** `lbn r? deviceHash nameHash logicType batchMode`
|
||||||
|
|
||||||
|
**Note:** This function is useful when a script interfaces with a lot of
|
||||||
|
devices, as it allows for arbitrary device access without limited to the 6 `dx` pins.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### Load Slot
|
||||||
|
|
||||||
|
`loadSlot(device, slotIndex, property)` / `ls(...)`
|
||||||
|
|
||||||
|
Loads a slot property from a device:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let occupied = loadSlot(sorter, 0, "Occupied");
|
||||||
|
let occupied = ls(sorter, 0, "Occupied");
|
||||||
|
```
|
||||||
|
|
||||||
|
**IC10:** `ls r? d? slotIndex logicSlotType`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### Load Reagent
|
||||||
|
|
||||||
|
`loadReagent(device, reagentMode, reagentHash)` / `lr(...)`
|
||||||
|
|
||||||
|
Loads reagent information from a device:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let amount = loadReagent(furnace, "Contents", reagentHash);
|
||||||
|
let amount = lr(furnace, "Contents", reagentHash);
|
||||||
|
```
|
||||||
|
|
||||||
|
**IC10:** `lr r? d? reagentMode reagentHash`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Writing to Devices
|
||||||
|
|
||||||
|
#### Set On Device
|
||||||
|
|
||||||
|
`set(device, property, value)` / `s(...)`
|
||||||
|
|
||||||
|
Sets a property on a device:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
set(valve, "On", true);
|
||||||
|
s(valve, "On", true);
|
||||||
|
|
||||||
|
// Preferred: use dot notation
|
||||||
|
valve.On = true;
|
||||||
|
```
|
||||||
|
|
||||||
|
**IC10:** `s d? logicType r?`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### Set On Device Batched
|
||||||
|
|
||||||
|
`setBatched(deviceHash, property, value)` / `sb(...)`
|
||||||
|
|
||||||
|
Sets a property on all devices matching a hash:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
const LIGHT_HASH = hash("StructureWallLight");
|
||||||
|
setBatched(LIGHT_HASH, "On", true);
|
||||||
|
sb(LIGHT_HASH, "On", true);
|
||||||
|
```
|
||||||
|
|
||||||
|
**IC10:** `sb deviceHash logicType r?`
|
||||||
|
|
||||||
|
**Note:** This function is useful when a script interfaces with a lot of devices,
|
||||||
|
as it allows for arbitrary device access without limited to the 6 `dx` pins.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### Set On Device Batched Named
|
||||||
|
|
||||||
|
`setBatchedNamed(deviceHash, nameHash, property, value)` / `sbn(...)`
|
||||||
|
|
||||||
|
Sets a property on devices matching both device hash and name hash:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
const SENSOR_HASH = hash("StructureGasSensor");
|
||||||
|
const SENSOR_NAME_HASH = hash("Outdoor Gas Sensor");
|
||||||
|
setBatchedNamed(SENSOR_HASH, SENSOR_NAME_HASH, "On", true);
|
||||||
|
sbn(SENSOR_HASH, SENSOR_NAME_HASH, "On", true);
|
||||||
|
```
|
||||||
|
|
||||||
|
**IC10:** `sbn deviceHash nameHash logicType r?`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
#### Set Slot
|
||||||
|
|
||||||
|
`setSlot(device, slotIndex, property, value)` / `ss(...)`
|
||||||
|
|
||||||
|
Sets a slot property on a device:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
setSlot(sorter, 0, "Open", true);
|
||||||
|
ss(sorter, 0, "Open", true);
|
||||||
|
```
|
||||||
|
|
||||||
|
**IC10:** `ss d? slotIndex logicSlotType r?`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Math Functions
|
||||||
|
|
||||||
|
All math functions accept numbers, variables, or expressions as arguments.
|
||||||
|
|
||||||
|
### Trigonometric Functions
|
||||||
|
|
||||||
|
| Function | Description | IC10 |
|
||||||
|
| ------------- | ---------------------------- | ------- |
|
||||||
|
| `sin(x)` | Sine of angle in radians | `sin` |
|
||||||
|
| `cos(x)` | Cosine of angle in radians | `cos` |
|
||||||
|
| `tan(x)` | Tangent of angle in radians | `tan` |
|
||||||
|
| `asin(x)` | Arc sine, returns radians | `asin` |
|
||||||
|
| `acos(x)` | Arc cosine, returns radians | `acos` |
|
||||||
|
| `atan(x)` | Arc tangent, returns radians | `atan` |
|
||||||
|
| `atan2(y, x)` | Two-argument arc tangent | `atan2` |
|
||||||
|
|
||||||
|
#### Trig Example
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let angle = atan2(y, x);
|
||||||
|
let sineValue = sin(angle);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Rounding Functions
|
||||||
|
|
||||||
|
| Function | Description | IC10 |
|
||||||
|
| ---------- | ----------------------------- | ------- |
|
||||||
|
| `ceil(x)` | Round up to nearest integer | `ceil` |
|
||||||
|
| `floor(x)` | Round down to nearest integer | `floor` |
|
||||||
|
| `trunc(x)` | Remove decimal portion | `trunc` |
|
||||||
|
| `abs(x)` | Absolute value | `abs` |
|
||||||
|
|
||||||
|
#### Rounding Example
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let rounded = floor(3.7); // 3
|
||||||
|
let positive = abs(-5); // 5
|
||||||
|
```
|
||||||
|
|
||||||
|
### Other Math Functions
|
||||||
|
|
||||||
|
| Function | Description | IC10 |
|
||||||
|
| ----------- | ----------------------------- | ------ |
|
||||||
|
| `sqrt(x)` | Square root | `sqrt` |
|
||||||
|
| `log(x)` | Natural logarithm | `log` |
|
||||||
|
| `max(a, b)` | Maximum of two values | `max` |
|
||||||
|
| `min(a, b)` | Minimum of two values | `min` |
|
||||||
|
| `rand()` | Random number between 0 and 1 | `rand` |
|
||||||
|
|
||||||
|
#### Math Example
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let root = sqrt(16); // 4
|
||||||
|
let bigger = max(a, b);
|
||||||
|
let randomVal = rand();
|
||||||
|
```
|
||||||
|
|
||||||
|
## See Also
|
||||||
|
|
||||||
|
- [Language Reference](language-reference.md) — Complete syntax guide
|
||||||
|
- [Examples](examples.md) — Real-world code samples
|
||||||
254
docs/examples.md
Normal file
254
docs/examples.md
Normal file
@@ -0,0 +1,254 @@
|
|||||||
|
# Examples
|
||||||
|
|
||||||
|
Real-world Slang programs demonstrating common patterns.
|
||||||
|
|
||||||
|
## Temperature Control
|
||||||
|
|
||||||
|
Basic thermostat that controls an air conditioner based on room temperature:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
device ac = "db";
|
||||||
|
device roomGasSensor = "d0";
|
||||||
|
|
||||||
|
const TARGET_TEMP = 22c;
|
||||||
|
const HYSTERESIS = 1;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
yield();
|
||||||
|
let temp = roomGasSensor.Temperature;
|
||||||
|
|
||||||
|
if (temp > TARGET_TEMP + HYSTERESIS) {
|
||||||
|
ac.On = true;
|
||||||
|
} else if (temp < TARGET_TEMP - HYSTERESIS) {
|
||||||
|
ac.On = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Note:** The IC10 chip is assumed to be inserted in the air conditioner's IC slot.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Two-Axis Solar Panel Tracking
|
||||||
|
|
||||||
|
Handles two-axis solar panel tracking based on the sun's position:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
device sensor = "d0";
|
||||||
|
|
||||||
|
const H_PANELS = hash("StructureSolarPanelDual");
|
||||||
|
|
||||||
|
loop {
|
||||||
|
setBatched(H_PANELS, "Horizontal", sensor.Horizontal);
|
||||||
|
setBatched(H_PANELS, "Vertical", sensor.Vertical + 90);
|
||||||
|
yield();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Note:** Assumes the daylight sensor is mounted with its port looking 90
|
||||||
|
degrees east of the solar panel's data port, an offset can be added on the
|
||||||
|
horizontal angle if needed.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Day/Night Lighting
|
||||||
|
|
||||||
|
Controls grow lights during the day and ambient lights at night:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
device greenhouseSensor = "d0";
|
||||||
|
|
||||||
|
const daylightSensor = hash("StructureDaylightSensor");
|
||||||
|
const growLight = hash("StructureGrowLight");
|
||||||
|
const wallLight = hash("StructureLightLong");
|
||||||
|
|
||||||
|
loop {
|
||||||
|
yield();
|
||||||
|
let solarAngle = lb(daylightSensor, "SolarAngle", "Average");
|
||||||
|
let isDaylight = solarAngle < 90;
|
||||||
|
|
||||||
|
sb(growLight, "On", isDaylight);
|
||||||
|
sb(wallLight, "On", !isDaylight);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Pressure Relief Valve
|
||||||
|
|
||||||
|
Controls a volume pump based on pressure readings for emergency pressure relief:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
device volumePump = "d0";
|
||||||
|
device pipeSensor = "d1";
|
||||||
|
|
||||||
|
const MAX_PRESSURE = 10_000;
|
||||||
|
const R = 8.314;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
yield();
|
||||||
|
|
||||||
|
let pressure = pipeSensor.Pressure;
|
||||||
|
|
||||||
|
if (pressure > MAX_PRESSURE) {
|
||||||
|
// Use PV=nRT to calculate the amount of mols we need to move
|
||||||
|
// n = PV / RT
|
||||||
|
let molsToMove = (pressure - MAX_PRESSURE) *
|
||||||
|
pipeSensor.Volume / (R * pipeSensor.Temperature);
|
||||||
|
|
||||||
|
// V = nRT / P
|
||||||
|
let setting = molsToMove * R * pipeSensor.Temperature / pressure;
|
||||||
|
volumePump.Setting = setting;
|
||||||
|
volumePump.On = true;
|
||||||
|
} else {
|
||||||
|
volumePump.On = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Greenhouse Environment Controller
|
||||||
|
|
||||||
|
Complete greenhouse control with pressure, temperature, and lighting:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
device self = "db";
|
||||||
|
device emergencyRelief = "d0";
|
||||||
|
device greenhouseSensor = "d1";
|
||||||
|
device recycleValve = "d2";
|
||||||
|
|
||||||
|
const MAX_INTERIOR_PRESSURE = 80;
|
||||||
|
const MAX_INTERIOR_TEMP = 28c;
|
||||||
|
const MIN_INTERIOR_PRESSURE = 75;
|
||||||
|
const MIN_INTERIOR_TEMP = 25c;
|
||||||
|
const daylightSensor = 1076425094;
|
||||||
|
const growLight = hash("StructureGrowLight");
|
||||||
|
const wallLight = hash("StructureLightLong");
|
||||||
|
const lightRound = hash("StructureLightRound");
|
||||||
|
|
||||||
|
let shouldPurge = false;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
yield();
|
||||||
|
let interiorPress = greenhouseSensor.Pressure;
|
||||||
|
let interiorTemp = greenhouseSensor.Temperature;
|
||||||
|
|
||||||
|
shouldPurge = (
|
||||||
|
interiorPress > MAX_INTERIOR_PRESSURE ||
|
||||||
|
interiorTemp > MAX_INTERIOR_TEMP
|
||||||
|
) || shouldPurge;
|
||||||
|
|
||||||
|
emergencyRelief.On = shouldPurge;
|
||||||
|
recycleValve.On = !shouldPurge;
|
||||||
|
|
||||||
|
if (
|
||||||
|
shouldPurge && (
|
||||||
|
interiorPress < MIN_INTERIOR_PRESSURE &&
|
||||||
|
interiorTemp < MIN_INTERIOR_TEMP
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
shouldPurge = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let solarAngle = lb(daylightSensor, "SolarAngle", "Average");
|
||||||
|
let isDaylight = solarAngle < 90;
|
||||||
|
|
||||||
|
sb(growLight, "On", isDaylight);
|
||||||
|
sb(wallLight, "On", !isDaylight);
|
||||||
|
sb(lightRound, "On", !isDaylight);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Advanced Furnace Pressure Control
|
||||||
|
|
||||||
|
Automates multi-furnace pump control based on dial setting for pressure target:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
const FURNACE1 = 1234;
|
||||||
|
const DIAL1 = 1123;
|
||||||
|
const ANALYZER1 = 1223;
|
||||||
|
|
||||||
|
const FURNACE2 = 1235;
|
||||||
|
const DIAL2 = 1124;
|
||||||
|
const ANALYZER2 = 1224;
|
||||||
|
|
||||||
|
const FURNACE3 = 1236;
|
||||||
|
const DIAL3 = 1124;
|
||||||
|
const ANALYZER3 = 1225;
|
||||||
|
|
||||||
|
const R = 8.314;
|
||||||
|
|
||||||
|
fn handleFurnace(furnace, dial, analyzer) {
|
||||||
|
let pressure = furnace.Pressure;
|
||||||
|
let targetPressure = max(dial.Setting, 0.1) * 1000;
|
||||||
|
|
||||||
|
if (abs(targetPressure - pressure) <= 0.1) {
|
||||||
|
furnace.On = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let molsToMove = max(furnace.TotalMoles, 1) * (
|
||||||
|
(targetPressure / pressure) - 1
|
||||||
|
);
|
||||||
|
|
||||||
|
// V = nRT / P
|
||||||
|
if (molsToMove > 0) {
|
||||||
|
// Calculate volume required
|
||||||
|
if (analyzer.Pressure == 0) {
|
||||||
|
// No more gas to add
|
||||||
|
furnace.On = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let volume = molsToMove * R * analyzer.Temperature / analyzer.Pressure;
|
||||||
|
|
||||||
|
furnace.On = true;
|
||||||
|
furnace.SettingOutput = 0;
|
||||||
|
furnace.SettingInput = volume;
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate volume required
|
||||||
|
let volume = (-molsToMove) * R * furnace.Temperature / pressure;
|
||||||
|
|
||||||
|
furnace.On = true;
|
||||||
|
furnace.SettingInput = 0;
|
||||||
|
furnace.SettingOutput = volume;
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
loop {
|
||||||
|
yield();
|
||||||
|
|
||||||
|
handleFurnace(FURNACE1, DIAL1, ANALYZER1);
|
||||||
|
handleFurnace(FURNACE2, DIAL2, ANALYZER2);
|
||||||
|
handleFurnace(FURNACE3, DIAL3, ANALYZER3);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Note:** This example does not handle edge cases such as insufficient gas in
|
||||||
|
the input network or overfilling the furnace/pipe network.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Common Patterns
|
||||||
|
|
||||||
|
### Waiting for a Condition
|
||||||
|
|
||||||
|
```rust
|
||||||
|
fn waitForDeviceToTurnOff(device) {
|
||||||
|
while (device.On) {
|
||||||
|
yield();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## See Also
|
||||||
|
|
||||||
|
- [Getting Started](getting-started.md) — First steps with Slang
|
||||||
|
- [Language Reference](language-reference.md) — Complete syntax guide
|
||||||
|
- [Built-in Functions](builtins.md) — System calls and math functions
|
||||||
99
docs/getting-started.md
Normal file
99
docs/getting-started.md
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
# Getting Started
|
||||||
|
|
||||||
|
<!--toc:start-->
|
||||||
|
|
||||||
|
- [Getting Started](#getting-started)
|
||||||
|
- [Program Structure](#program-structure)
|
||||||
|
- [The `yield()` Function](#the-yield-function)
|
||||||
|
- [Your First Program](#your-first-program)
|
||||||
|
- [Explanation](#explanation)
|
||||||
|
- [Comments](#comments)
|
||||||
|
- [See Also](#see-also)
|
||||||
|
<!--toc:end-->
|
||||||
|
|
||||||
|
This guide covers the basics of writing your first Slang program.
|
||||||
|
|
||||||
|
## Program Structure
|
||||||
|
|
||||||
|
A Slang program consists of top-level declarations and a main loop:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// Device declarations
|
||||||
|
device self = "db";
|
||||||
|
device sensor = "d0";
|
||||||
|
|
||||||
|
// Constants
|
||||||
|
const THRESHOLD = 100;
|
||||||
|
|
||||||
|
// Variables
|
||||||
|
let counter = 0;
|
||||||
|
|
||||||
|
// Main program loop
|
||||||
|
loop {
|
||||||
|
yield();
|
||||||
|
// Your logic here
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## The `yield()` Function
|
||||||
|
|
||||||
|
IC10 programs run continuously. The `yield()` function pauses execution for one
|
||||||
|
game tick, preventing the script from consuming excessive resources.
|
||||||
|
|
||||||
|
**Important:** You should always include `yield()` in your main loop unless you
|
||||||
|
know what you're doing.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
loop {
|
||||||
|
yield(); // Recommended!
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Your First Program
|
||||||
|
|
||||||
|
Here's a simple program that turns on a light when a gas sensor detects low
|
||||||
|
pressure:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
device gasSensor = "d0";
|
||||||
|
device light = "d1";
|
||||||
|
|
||||||
|
const LOW_PRESSURE = 50;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
yield();
|
||||||
|
light.On = gasSensor.Pressure < LOW_PRESSURE;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Explanation
|
||||||
|
|
||||||
|
1. `device gasSensor = "d0"` — Binds the device at port `d0` to the name
|
||||||
|
`gasSensor`
|
||||||
|
2. `device light = "d1"` — Binds the device at port `d1` to the name `light`
|
||||||
|
3. `const LOW_PRESSURE = 50` — Defines a compile-time constant
|
||||||
|
4. `loop { ... }` — Creates an infinite loop
|
||||||
|
5. `yield()` — Pauses for one tick
|
||||||
|
6. `light.On = gasSensor.Pressure < LOW_PRESSURE` — Reads the pressure and sets
|
||||||
|
the light state
|
||||||
|
|
||||||
|
## Comments
|
||||||
|
|
||||||
|
Slang supports single-line comments and documentation comments:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// This is a regular comment
|
||||||
|
|
||||||
|
/// This is a documentation comment
|
||||||
|
/// It can span multiple lines
|
||||||
|
fn myFunction() {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## See Also
|
||||||
|
|
||||||
|
- [Language Reference](language-reference.md) — Complete syntax guide
|
||||||
|
- [Built-in Functions](builtins.md) — Available system calls
|
||||||
|
- [Examples](examples.md) — Real-world programs and patterns
|
||||||
339
docs/language-reference.md
Normal file
339
docs/language-reference.md
Normal file
@@ -0,0 +1,339 @@
|
|||||||
|
# Language Reference
|
||||||
|
|
||||||
|
<!--toc:start-->
|
||||||
|
|
||||||
|
- [Language Reference](#language-reference)
|
||||||
|
- [Literals](#literals)
|
||||||
|
- [Numbers](#numbers)
|
||||||
|
- [Temperature Literals](#temperature-literals)
|
||||||
|
- [Booleans](#booleans)
|
||||||
|
- [Strings](#strings)
|
||||||
|
- [Variables](#variables)
|
||||||
|
- [`let` - Mutable Variables](#let-mutable-variables)
|
||||||
|
- [`const` - Constants](#const-constants)
|
||||||
|
- [Device Declarations](#device-declarations)
|
||||||
|
- [Device Property Access](#device-property-access)
|
||||||
|
- [Device Property Assignment](#device-property-assignment)
|
||||||
|
- [Operators](#operators)
|
||||||
|
- [Arithmetic Operators](#arithmetic-operators)
|
||||||
|
- [Comparison Operators](#comparison-operators)
|
||||||
|
- [Logical Operators](#logical-operators)
|
||||||
|
- [Ternary Operator](#ternary-operator)
|
||||||
|
- [Operator Precedence](#operator-precedence)
|
||||||
|
- [Control Flow](#control-flow)
|
||||||
|
- [`if` / `else`](#if-and-else)
|
||||||
|
- [`loop`](#loop)
|
||||||
|
- [`while`](#while)
|
||||||
|
- [`break`](#break)
|
||||||
|
- [`continue`](#continue)
|
||||||
|
- [Functions](#functions)
|
||||||
|
- [Declaration](#declaration)
|
||||||
|
- [Invocation](#invocation)
|
||||||
|
- [Return Values](#return-values)
|
||||||
|
- [Parentheses for Grouping](#parentheses-for-grouping)
|
||||||
|
- [See Also](#see-also)
|
||||||
|
<!--toc:end-->
|
||||||
|
|
||||||
|
Complete syntax reference for the Slang programming language.
|
||||||
|
|
||||||
|
## Literals
|
||||||
|
|
||||||
|
### Numbers
|
||||||
|
|
||||||
|
Numbers can be integers or decimals. Underscores are allowed as visual
|
||||||
|
separators:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
const integer = 42; // Integer
|
||||||
|
const decimal = 3.14; // Decimal
|
||||||
|
const million = 1_000_000; // Integer with separators
|
||||||
|
const decimalSeparators = 5_000.50; // Decimal with separators
|
||||||
|
```
|
||||||
|
|
||||||
|
### Temperature Literals
|
||||||
|
|
||||||
|
Append a unit suffix to specify temperature. Values are automatically converted
|
||||||
|
to Kelvin at compile time:
|
||||||
|
|
||||||
|
| Suffix | Unit | Example |
|
||||||
|
| ------ | ---------- | ------- |
|
||||||
|
| `c` | Celsius | `20c` |
|
||||||
|
| `f` | Fahrenheit | `68f` |
|
||||||
|
| `k` | Kelvin | `293k` |
|
||||||
|
|
||||||
|
```rust
|
||||||
|
const ROOM_TEMP = 20c; // Converts to 293.15 Kelvin
|
||||||
|
const FREEZING = 32f; // Converts to 273.15 Kelvin
|
||||||
|
const ABSOLUTE1 = 0k; // Already in Kelvin
|
||||||
|
const ABSOLUTE2 = 0; // Assumed to be in Kelvin
|
||||||
|
```
|
||||||
|
|
||||||
|
### Booleans
|
||||||
|
|
||||||
|
Booleans compile to integer values `1` and `0` in IC10.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
device ac = "d0";
|
||||||
|
|
||||||
|
ac.Mode = false;
|
||||||
|
ac.On = true;
|
||||||
|
```
|
||||||
|
|
||||||
|
### Strings
|
||||||
|
|
||||||
|
Strings use double or single quotes. They are primarily used for prefab and
|
||||||
|
name hashes.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
const AC_HASH = hash("StructureAirConditioner");
|
||||||
|
const AC_NAME_HASH = hash("Greenhouse Air Conditioner");
|
||||||
|
```
|
||||||
|
|
||||||
|
## Variables
|
||||||
|
|
||||||
|
### `let` Mutable Variables
|
||||||
|
|
||||||
|
Declares a variable that can be reassigned:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let counter = 0;
|
||||||
|
// ...
|
||||||
|
counter = counter + 1;
|
||||||
|
```
|
||||||
|
|
||||||
|
### `const` Constants
|
||||||
|
|
||||||
|
Declares a compile-time constant. Constants are inlined and do not consume
|
||||||
|
registers:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
const MAX_PRESSURE = 10_000;
|
||||||
|
const DOOR_HASH = hash("StructureCompositeDoor");
|
||||||
|
```
|
||||||
|
|
||||||
|
Constants support the `hash()` function for compile-time hash computation.
|
||||||
|
|
||||||
|
## Device Declarations
|
||||||
|
|
||||||
|
The `device` keyword binds a device port or reference ID to a named variable:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
device self = "db"; // IC housing, or device the IC is plugged into (eg. an AC)
|
||||||
|
device sensor = "d0"; // Device at port d0
|
||||||
|
device valve = "d1"; // Device at port d1
|
||||||
|
device ac1 = "$3FC"; // Device with reference ID $3FC (hexadecimal 1020)
|
||||||
|
device ac2 = "1020"; // Device with reference ID 1020 (decimal)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Note:** Reference IDs can be found in-game using the Configuration cartridge.
|
||||||
|
|
||||||
|
### Device Property Access
|
||||||
|
|
||||||
|
Read device properties using dot notation:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let temp = sensor.Temperature;
|
||||||
|
let pressure = sensor.Pressure;
|
||||||
|
let isOn = valve.On;
|
||||||
|
```
|
||||||
|
|
||||||
|
### Device Property Assignment
|
||||||
|
|
||||||
|
Write to device properties using dot notation:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
valve.On = true;
|
||||||
|
valve.Setting = 100;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Operators
|
||||||
|
|
||||||
|
### Arithmetic Operators
|
||||||
|
|
||||||
|
| Operator | Description | Example |
|
||||||
|
| -------- | -------------- | -------- |
|
||||||
|
| `+` | Addition | `a + b` |
|
||||||
|
| `-` | Subtraction | `a - b` |
|
||||||
|
| `*` | Multiplication | `a * b` |
|
||||||
|
| `/` | Division | `a / b` |
|
||||||
|
| `%` | Modulo | `a % b` |
|
||||||
|
| `**` | Exponentiation | `a ** b` |
|
||||||
|
| `-` | Negation | `-a` |
|
||||||
|
|
||||||
|
### Comparison Operators
|
||||||
|
|
||||||
|
| Operator | Description | Example |
|
||||||
|
| -------- | --------------------- | -------- |
|
||||||
|
| `==` | Equal | `a == b` |
|
||||||
|
| `!=` | Not equal | `a != b` |
|
||||||
|
| `<` | Less than | `a < b` |
|
||||||
|
| `>` | Greater than | `a > b` |
|
||||||
|
| `<=` | Less than or equal | `a <= b` |
|
||||||
|
| `>=` | Greater than or equal | `a >= b` |
|
||||||
|
|
||||||
|
### Logical Operators
|
||||||
|
|
||||||
|
| Operator | Description | Example |
|
||||||
|
| -------- | ----------- | ---------- |
|
||||||
|
| `&&` | Logical AND | `a && b` |
|
||||||
|
| `\|\|` | Logical OR | `a \|\| b` |
|
||||||
|
| `!` | Logical NOT | `!a` |
|
||||||
|
|
||||||
|
### Ternary Operator
|
||||||
|
|
||||||
|
Conditional expressions using `?` and `:`:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let result = condition ? valueIfTrue : valueIfFalse;
|
||||||
|
```
|
||||||
|
|
||||||
|
### Operator Precedence
|
||||||
|
|
||||||
|
Operators are evaluated in the following order, from highest to lowest
|
||||||
|
precedence:
|
||||||
|
|
||||||
|
| Precedence | Operator(s) | Description |
|
||||||
|
| ---------- | ----------------- | -------------------------------- |
|
||||||
|
| 1 | `()` `.` | Grouping, Property access |
|
||||||
|
| 2 | `!` `-` | Logical NOT, Negation |
|
||||||
|
| 3 | `**` | Exponentiation |
|
||||||
|
| 4 | `*` `/` `%` | Multiplication, Division, Modulo |
|
||||||
|
| 5 | `+` `-` | Addition, Subtraction |
|
||||||
|
| 6 | `<` `<=` `>` `>=` | Comparison |
|
||||||
|
| 7 | `==` `!=` | Equality |
|
||||||
|
| 8 | `&&` | Logical AND |
|
||||||
|
| 9 | `\|\|` | Logical OR |
|
||||||
|
| 10 | `?:` | Ternary conditional |
|
||||||
|
| 11 | `=` | Assignment |
|
||||||
|
|
||||||
|
Use parentheses to override precedence:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let result = (20 + 10) * 5;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Control Flow
|
||||||
|
|
||||||
|
### if and else
|
||||||
|
|
||||||
|
Conditional branching:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
if (tank.Temperature > 30c) {
|
||||||
|
ac.On = true;
|
||||||
|
} else {
|
||||||
|
ac.On = false;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### `loop`
|
||||||
|
|
||||||
|
Infinite loop that runs until `break`:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
loop {
|
||||||
|
yield();
|
||||||
|
// Loop body
|
||||||
|
if (condition) {
|
||||||
|
break; // Exit the loop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### `while`
|
||||||
|
|
||||||
|
Conditional loop that runs while the condition is true:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
while (counter < 100) {
|
||||||
|
counter = counter + 1;
|
||||||
|
yield();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### `break`
|
||||||
|
|
||||||
|
Exits the current loop:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
loop {
|
||||||
|
yield();
|
||||||
|
// ...
|
||||||
|
if (done) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### `continue`
|
||||||
|
|
||||||
|
Skips to the next iteration of the current loop:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
loop {
|
||||||
|
yield();
|
||||||
|
if (shouldSkip) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// This code is skipped when shouldSkip is true
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Functions
|
||||||
|
|
||||||
|
**Warning:** Functions are currently experimental and may produce suboptimal code.
|
||||||
|
|
||||||
|
### Declaration
|
||||||
|
|
||||||
|
```rust
|
||||||
|
fn functionName(arg1, arg2) {
|
||||||
|
// Function body
|
||||||
|
return arg1 + arg2;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Invocation
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let result = functionName(10, 20);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Return Values
|
||||||
|
|
||||||
|
Use `return` to exit a function and optionally return a value:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
fn calculate(x) {
|
||||||
|
if (x < 0) {
|
||||||
|
return 0; // Early return
|
||||||
|
}
|
||||||
|
|
||||||
|
return x * 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn doWork() {
|
||||||
|
// No return value
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Parentheses for Grouping
|
||||||
|
|
||||||
|
Use parentheses to control operator precedence:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let result = (a + b) * c;
|
||||||
|
|
||||||
|
let complex = (
|
||||||
|
temp > 0c &&
|
||||||
|
stress < 50 &&
|
||||||
|
(pressure < 10_000 || temp > 20c)
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
## See Also
|
||||||
|
|
||||||
|
- [Getting Started](getting-started.md) — First steps with Slang
|
||||||
|
- [Built-in Functions](builtins.md) — System calls and math functions
|
||||||
|
- [Examples](examples.md) — Real-world code samples
|
||||||
2
rust_compiler/Cargo.lock
generated
2
rust_compiler/Cargo.lock
generated
@@ -930,7 +930,7 @@ checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "slang"
|
name = "slang"
|
||||||
version = "0.4.3"
|
version = "0.5.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"clap",
|
"clap",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "slang"
|
name = "slang"
|
||||||
version = "0.4.3"
|
version = "0.5.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
|
|||||||
@@ -4,15 +4,21 @@ use pretty_assertions::assert_eq;
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn simple_binary_expression() -> Result<()> {
|
fn simple_binary_expression() -> Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let i = 1 + 2;
|
let i = 1 + 2;
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -27,8 +33,8 @@ fn simple_binary_expression() -> Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn nested_binary_expressions() -> Result<()> {
|
fn nested_binary_expressions() -> Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
fn calculateArgs(arg1, arg2, arg3) {
|
fn calculateArgs(arg1, arg2, arg3) {
|
||||||
return (arg1 + arg2) * arg3;
|
return (arg1 + arg2) * arg3;
|
||||||
@@ -38,8 +44,14 @@ fn nested_binary_expressions() -> Result<()> {
|
|||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -47,6 +59,7 @@ fn nested_binary_expressions() -> Result<()> {
|
|||||||
pop r8
|
pop r8
|
||||||
pop r9
|
pop r9
|
||||||
pop r10
|
pop r10
|
||||||
|
push sp
|
||||||
push ra
|
push ra
|
||||||
add r1 r10 r9
|
add r1 r10 r9
|
||||||
mul r2 r1 r8
|
mul r2 r1 r8
|
||||||
@@ -54,6 +67,7 @@ fn nested_binary_expressions() -> Result<()> {
|
|||||||
j __internal_L1
|
j __internal_L1
|
||||||
__internal_L1:
|
__internal_L1:
|
||||||
pop ra
|
pop ra
|
||||||
|
pop sp
|
||||||
j ra
|
j ra
|
||||||
main:
|
main:
|
||||||
push 10
|
push 10
|
||||||
@@ -72,15 +86,21 @@ fn nested_binary_expressions() -> Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn stress_test_constant_folding() -> Result<()> {
|
fn stress_test_constant_folding() -> Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let negationHell = (-1 + -2) * (-3 + (-4 * (-5 + -6)));
|
let negationHell = (-1 + -2) * (-3 + (-4 * (-5 + -6)));
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -95,16 +115,22 @@ fn stress_test_constant_folding() -> Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_constant_folding_with_variables_mixed_in() -> Result<()> {
|
fn test_constant_folding_with_variables_mixed_in() -> Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
r#"
|
r#"
|
||||||
device self = "db";
|
device self = "db";
|
||||||
let i = 1 - 3 * (1 + 123.4) * self.Setting + 245c;
|
let i = 1 - 3 * (1 + 123.4) * self.Setting + 245c;
|
||||||
"#
|
"#
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -123,15 +149,21 @@ fn test_constant_folding_with_variables_mixed_in() -> Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_ternary_expression() -> Result<()> {
|
fn test_ternary_expression() -> Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
r#"
|
r#"
|
||||||
let i = 1 > 2 ? 15 : 20;
|
let i = 1 > 2 ? 15 : 20;
|
||||||
"#
|
"#
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -148,16 +180,22 @@ fn test_ternary_expression() -> Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_ternary_expression_assignment() -> Result<()> {
|
fn test_ternary_expression_assignment() -> Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
r#"
|
r#"
|
||||||
let i = 0;
|
let i = 0;
|
||||||
i = 1 > 2 ? 15 : 20;
|
i = 1 > 2 ? 15 : 20;
|
||||||
"#
|
"#
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -175,15 +213,21 @@ fn test_ternary_expression_assignment() -> Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_negative_literals() -> Result<()> {
|
fn test_negative_literals() -> Result<()> {
|
||||||
let compiled = compile!(
|
let result = compile!(
|
||||||
debug
|
check
|
||||||
r#"
|
r#"
|
||||||
let item = -10c - 20c;
|
let item = -10c - 20c;
|
||||||
"#
|
"#
|
||||||
);
|
);
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -198,16 +242,22 @@ fn test_negative_literals() -> Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_mismatched_temperature_literals() -> Result<()> {
|
fn test_mismatched_temperature_literals() -> Result<()> {
|
||||||
let compiled = compile!(
|
let result = compile!(
|
||||||
debug
|
check
|
||||||
r#"
|
r#"
|
||||||
let item = -10c - 100k;
|
let item = -10c - 100k;
|
||||||
let item2 = item + 500c;
|
let item2 = item + 500c;
|
||||||
"#
|
"#
|
||||||
);
|
);
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ use pretty_assertions::assert_eq;
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_if_statement() -> anyhow::Result<()> {
|
fn test_if_statement() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let a = 10;
|
let a = 10;
|
||||||
if (a > 5) {
|
if (a > 5) {
|
||||||
@@ -13,8 +13,14 @@ fn test_if_statement() -> anyhow::Result<()> {
|
|||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -33,8 +39,8 @@ fn test_if_statement() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_if_else_statement() -> anyhow::Result<()> {
|
fn test_if_else_statement() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let a = 0;
|
let a = 0;
|
||||||
if (10 > 5) {
|
if (10 > 5) {
|
||||||
@@ -45,8 +51,14 @@ fn test_if_else_statement() -> anyhow::Result<()> {
|
|||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -68,8 +80,8 @@ fn test_if_else_statement() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_if_else_if_statement() -> anyhow::Result<()> {
|
fn test_if_else_if_statement() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let a = 0;
|
let a = 0;
|
||||||
if (a == 1) {
|
if (a == 1) {
|
||||||
@@ -82,8 +94,14 @@ fn test_if_else_if_statement() -> anyhow::Result<()> {
|
|||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -111,8 +129,8 @@ fn test_if_else_if_statement() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_spilled_variable_update_in_branch() -> anyhow::Result<()> {
|
fn test_spilled_variable_update_in_branch() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let a = 1;
|
let a = 1;
|
||||||
let b = 2;
|
let b = 2;
|
||||||
@@ -129,8 +147,14 @@ fn test_spilled_variable_update_in_branch() -> anyhow::Result<()> {
|
|||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
|
|||||||
@@ -3,21 +3,29 @@ use pretty_assertions::assert_eq;
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn no_arguments() -> anyhow::Result<()> {
|
fn no_arguments() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
fn doSomething() {};
|
fn doSomething() {};
|
||||||
let i = doSomething();
|
let i = doSomething();
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
let to_test = indoc! {
|
let to_test = indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
doSomething:
|
doSomething:
|
||||||
|
push sp
|
||||||
push ra
|
push ra
|
||||||
__internal_L1:
|
__internal_L1:
|
||||||
pop ra
|
pop ra
|
||||||
|
pop sp
|
||||||
j ra
|
j ra
|
||||||
main:
|
main:
|
||||||
jal doSomething
|
jal doSomething
|
||||||
@@ -25,15 +33,15 @@ fn no_arguments() -> anyhow::Result<()> {
|
|||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(compiled, to_test);
|
assert_eq!(result.output, to_test);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn let_var_args() -> anyhow::Result<()> {
|
fn let_var_args() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
fn mul2(arg1) {
|
fn mul2(arg1) {
|
||||||
return arg1 * 2;
|
return arg1 * 2;
|
||||||
@@ -46,19 +54,27 @@ fn let_var_args() -> anyhow::Result<()> {
|
|||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
mul2:
|
mul2:
|
||||||
pop r8
|
pop r8
|
||||||
|
push sp
|
||||||
push ra
|
push ra
|
||||||
mul r1 r8 2
|
mul r1 r8 2
|
||||||
move r15 r1
|
move r15 r1
|
||||||
j __internal_L1
|
j __internal_L1
|
||||||
__internal_L1:
|
__internal_L1:
|
||||||
pop ra
|
pop ra
|
||||||
|
pop sp
|
||||||
j ra
|
j ra
|
||||||
main:
|
main:
|
||||||
__internal_L2:
|
__internal_L2:
|
||||||
@@ -99,8 +115,8 @@ fn incorrect_args_count() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn inline_literal_args() -> anyhow::Result<()> {
|
fn inline_literal_args() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
fn doSomething(arg1, arg2) {
|
fn doSomething(arg1, arg2) {
|
||||||
return 5;
|
return 5;
|
||||||
@@ -110,19 +126,27 @@ fn inline_literal_args() -> anyhow::Result<()> {
|
|||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
doSomething:
|
doSomething:
|
||||||
pop r8
|
pop r8
|
||||||
pop r9
|
pop r9
|
||||||
|
push sp
|
||||||
push ra
|
push ra
|
||||||
move r15 5
|
move r15 5
|
||||||
j __internal_L1
|
j __internal_L1
|
||||||
__internal_L1:
|
__internal_L1:
|
||||||
pop ra
|
pop ra
|
||||||
|
pop sp
|
||||||
j ra
|
j ra
|
||||||
main:
|
main:
|
||||||
move r8 123
|
move r8 123
|
||||||
@@ -141,8 +165,8 @@ fn inline_literal_args() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn mixed_args() -> anyhow::Result<()> {
|
fn mixed_args() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let arg1 = 123;
|
let arg1 = 123;
|
||||||
let returnValue = doSomething(arg1, 456);
|
let returnValue = doSomething(arg1, 456);
|
||||||
@@ -150,17 +174,25 @@ fn mixed_args() -> anyhow::Result<()> {
|
|||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
doSomething:
|
doSomething:
|
||||||
pop r8
|
pop r8
|
||||||
pop r9
|
pop r9
|
||||||
|
push sp
|
||||||
push ra
|
push ra
|
||||||
__internal_L1:
|
__internal_L1:
|
||||||
pop ra
|
pop ra
|
||||||
|
pop sp
|
||||||
j ra
|
j ra
|
||||||
main:
|
main:
|
||||||
move r8 123
|
move r8 123
|
||||||
@@ -179,8 +211,8 @@ fn mixed_args() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn with_return_statement() -> anyhow::Result<()> {
|
fn with_return_statement() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
fn doSomething(arg1) {
|
fn doSomething(arg1) {
|
||||||
return 456;
|
return 456;
|
||||||
@@ -190,18 +222,26 @@ fn with_return_statement() -> anyhow::Result<()> {
|
|||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
doSomething:
|
doSomething:
|
||||||
pop r8
|
pop r8
|
||||||
|
push sp
|
||||||
push ra
|
push ra
|
||||||
move r15 456
|
move r15 456
|
||||||
j __internal_L1
|
j __internal_L1
|
||||||
__internal_L1:
|
__internal_L1:
|
||||||
pop ra
|
pop ra
|
||||||
|
pop sp
|
||||||
j ra
|
j ra
|
||||||
main:
|
main:
|
||||||
push 123
|
push 123
|
||||||
@@ -216,8 +256,8 @@ fn with_return_statement() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn with_negative_return_literal() -> anyhow::Result<()> {
|
fn with_negative_return_literal() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
fn doSomething() {
|
fn doSomething() {
|
||||||
return -1;
|
return -1;
|
||||||
@@ -226,16 +266,24 @@ fn with_negative_return_literal() -> anyhow::Result<()> {
|
|||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
doSomething:
|
doSomething:
|
||||||
|
push sp
|
||||||
push ra
|
push ra
|
||||||
move r15 -1
|
move r15 -1
|
||||||
__internal_L1:
|
__internal_L1:
|
||||||
pop ra
|
pop ra
|
||||||
|
pop sp
|
||||||
j ra
|
j ra
|
||||||
main:
|
main:
|
||||||
jal doSomething
|
jal doSomething
|
||||||
|
|||||||
@@ -4,13 +4,19 @@ use pretty_assertions::assert_eq;
|
|||||||
#[test]
|
#[test]
|
||||||
fn variable_declaration_numeric_literal() -> anyhow::Result<()> {
|
fn variable_declaration_numeric_literal() -> anyhow::Result<()> {
|
||||||
let compiled = crate::compile! {
|
let compiled = crate::compile! {
|
||||||
debug r#"
|
check r#"
|
||||||
let i = 20c;
|
let i = 20c;
|
||||||
"#
|
"#
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -26,7 +32,7 @@ fn variable_declaration_numeric_literal() -> anyhow::Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn variable_declaration_numeric_literal_stack_spillover() -> anyhow::Result<()> {
|
fn variable_declaration_numeric_literal_stack_spillover() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
r#"
|
r#"
|
||||||
let a = 0;
|
let a = 0;
|
||||||
let b = 1;
|
let b = 1;
|
||||||
@@ -40,8 +46,14 @@ fn variable_declaration_numeric_literal_stack_spillover() -> anyhow::Result<()>
|
|||||||
let j = 9;
|
let j = 9;
|
||||||
"#};
|
"#};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -67,14 +79,20 @@ fn variable_declaration_numeric_literal_stack_spillover() -> anyhow::Result<()>
|
|||||||
#[test]
|
#[test]
|
||||||
fn variable_declaration_negative() -> anyhow::Result<()> {
|
fn variable_declaration_negative() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let i = -1;
|
let i = -1;
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -90,15 +108,21 @@ fn variable_declaration_negative() -> anyhow::Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_boolean_declaration() -> anyhow::Result<()> {
|
fn test_boolean_declaration() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let t = true;
|
let t = true;
|
||||||
let f = false;
|
let f = false;
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -115,7 +139,7 @@ fn test_boolean_declaration() -> anyhow::Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_boolean_return() -> anyhow::Result<()> {
|
fn test_boolean_return() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
fn getTrue() {
|
fn getTrue() {
|
||||||
return true;
|
return true;
|
||||||
@@ -125,17 +149,25 @@ fn test_boolean_return() -> anyhow::Result<()> {
|
|||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
getTrue:
|
getTrue:
|
||||||
|
push sp
|
||||||
push ra
|
push ra
|
||||||
move r15 1
|
move r15 1
|
||||||
j __internal_L1
|
j __internal_L1
|
||||||
__internal_L1:
|
__internal_L1:
|
||||||
pop ra
|
pop ra
|
||||||
|
pop sp
|
||||||
j ra
|
j ra
|
||||||
main:
|
main:
|
||||||
jal getTrue
|
jal getTrue
|
||||||
@@ -149,15 +181,21 @@ fn test_boolean_return() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_const_hash_expr() -> anyhow::Result<()> {
|
fn test_const_hash_expr() -> anyhow::Result<()> {
|
||||||
let compiled = compile!(debug r#"
|
let compiled = compile!(check r#"
|
||||||
const nameHash = hash("AccessCard");
|
const nameHash = hash("AccessCard");
|
||||||
device self = "db";
|
device self = "db";
|
||||||
|
|
||||||
self.Setting = nameHash;
|
self.Setting = nameHash;
|
||||||
"#);
|
"#);
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -168,3 +206,34 @@ fn test_const_hash_expr() -> anyhow::Result<()> {
|
|||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_declaration_is_const() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check
|
||||||
|
r#"
|
||||||
|
const MAX = 100;
|
||||||
|
|
||||||
|
let max = MAX;
|
||||||
|
"#
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 100
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
274
rust_compiler/libs/compiler/src/test/device_access.rs
Normal file
274
rust_compiler/libs/compiler/src/test/device_access.rs
Normal file
@@ -0,0 +1,274 @@
|
|||||||
|
use indoc::indoc;
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn device_declaration() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
device d0 = \"d0\";
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Declaration only emits the jump label header
|
||||||
|
assert_eq!(compiled.output, "j main\n");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn device_property_read() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
device ac = \"d0\";
|
||||||
|
let temp = ac.Temperature;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
l r1 d0 Temperature
|
||||||
|
move r8 r1
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn device_property_write() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
device ac = \"d0\";
|
||||||
|
ac.On = 1;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
s d0 On 1
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn multiple_device_declarations() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
device d0 = \"d0\";
|
||||||
|
device d1 = \"d1\";
|
||||||
|
device d2 = \"d2\";
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
// Declarations only emit the header when unused
|
||||||
|
assert_eq!(compiled.output, "j main\n");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn device_with_variable_interaction() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
device sensor = \"d0\";
|
||||||
|
let reading = sensor.Temperature;
|
||||||
|
let threshold = 373.15;
|
||||||
|
let alert = reading > threshold;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
l r1 d0 Temperature
|
||||||
|
move r8 r1
|
||||||
|
move r9 373.15
|
||||||
|
sgt r2 r8 r9
|
||||||
|
move r10 r2
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn device_property_in_arithmetic() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
device d0 = \"d0\";
|
||||||
|
let result = d0.Temperature + 100;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
// Verify that we load property, add 100, and move to result
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
l r1 d0 Temperature
|
||||||
|
add r2 r1 100
|
||||||
|
move r8 r2
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn device_used_in_function() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
device d0 = \"d0\";
|
||||||
|
|
||||||
|
fn check_power() {
|
||||||
|
return d0.On;
|
||||||
|
};
|
||||||
|
|
||||||
|
let powered = check_power();
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
check_power:
|
||||||
|
push sp
|
||||||
|
push ra
|
||||||
|
l r1 d0 On
|
||||||
|
move r15 r1
|
||||||
|
j __internal_L1
|
||||||
|
__internal_L1:
|
||||||
|
pop ra
|
||||||
|
pop sp
|
||||||
|
j ra
|
||||||
|
main:
|
||||||
|
jal check_power
|
||||||
|
move r8 r15
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn device_in_conditional() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
device d0 = \"d0\";
|
||||||
|
|
||||||
|
if (d0.On) {
|
||||||
|
let x = 1;
|
||||||
|
}
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
l r1 d0 On
|
||||||
|
beqz r1 __internal_L1
|
||||||
|
move r8 1
|
||||||
|
__internal_L1:
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn device_property_with_underscore_name() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
device cool_device = \"d0\";
|
||||||
|
let value = cool_device.SomeProperty;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
l r1 d0 SomeProperty
|
||||||
|
move r8 r1
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
737
rust_compiler/libs/compiler/src/test/edge_cases.rs
Normal file
737
rust_compiler/libs/compiler/src/test/edge_cases.rs
Normal file
@@ -0,0 +1,737 @@
|
|||||||
|
use indoc::indoc;
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn zero_value_handling() -> anyhow::Result<()> {
|
||||||
|
let result = compile! {
|
||||||
|
check "
|
||||||
|
let x = 0;
|
||||||
|
let y = x + 0;
|
||||||
|
let z = x * 100;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
result.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 0
|
||||||
|
add r1 r8 0
|
||||||
|
move r9 r1
|
||||||
|
mul r2 r8 100
|
||||||
|
move r10 r2
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn negative_number_handling() -> anyhow::Result<()> {
|
||||||
|
let result = compile! {
|
||||||
|
check "
|
||||||
|
let x = -100;
|
||||||
|
let y = -x;
|
||||||
|
let z = -(-50);
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
result.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 -100
|
||||||
|
sub r1 0 r8
|
||||||
|
move r9 r1
|
||||||
|
move r10 50
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn large_number_constants() -> anyhow::Result<()> {
|
||||||
|
let result = compile! {
|
||||||
|
check "
|
||||||
|
let x = 999999999;
|
||||||
|
let y = x + 1;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
result.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 999999999
|
||||||
|
add r1 r8 1
|
||||||
|
move r9 r1
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn floating_point_precision() -> anyhow::Result<()> {
|
||||||
|
let result = compile! {
|
||||||
|
check "
|
||||||
|
let pi = 3.14159265;
|
||||||
|
let e = 2.71828182;
|
||||||
|
let sum = pi + e;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
result.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 3.14159265
|
||||||
|
move r9 2.71828182
|
||||||
|
add r1 r8 r9
|
||||||
|
move r10 r1
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn temperature_unit_conversion() -> anyhow::Result<()> {
|
||||||
|
let result = compile! {
|
||||||
|
check "
|
||||||
|
let celsius = 20c;
|
||||||
|
let fahrenheit = 68f;
|
||||||
|
let kelvin = 293.15k;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
result.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 293.15
|
||||||
|
move r9 293.15
|
||||||
|
move r10 293.15
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn mixed_temperature_units() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let c = 0c;
|
||||||
|
let f = 32f;
|
||||||
|
let k = 273.15k;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 273.15
|
||||||
|
move r9 273.15
|
||||||
|
move r10 273.15
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn boolean_constant_folding() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = true;
|
||||||
|
let y = false;
|
||||||
|
let z = true && true;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 1
|
||||||
|
move r9 0
|
||||||
|
and r1 1 1
|
||||||
|
move r10 r1
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn empty_block() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = 5;
|
||||||
|
{
|
||||||
|
}
|
||||||
|
let y = x;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 5
|
||||||
|
move r9 r8
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn multiple_statements_same_line() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = 1; let y = 2; let z = 3;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 1
|
||||||
|
move r9 2
|
||||||
|
move r10 3
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn function_with_no_return() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
fn no_return() {
|
||||||
|
let x = 5;
|
||||||
|
};
|
||||||
|
|
||||||
|
no_return();
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
no_return:
|
||||||
|
push sp
|
||||||
|
push ra
|
||||||
|
move r8 5
|
||||||
|
__internal_L1:
|
||||||
|
pop ra
|
||||||
|
pop sp
|
||||||
|
j ra
|
||||||
|
main:
|
||||||
|
jal no_return
|
||||||
|
move r1 r15
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn deeply_nested_expressions() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = ((((((((1 + 2) + 3) + 4) + 5) + 6) + 7) + 8) + 9);
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 45
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn constant_folding_with_operations() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = 10 * 5 + 3 - 2;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 51
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn constant_folding_with_division() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = 100 / 2 / 5;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 10
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn modulo_operation() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = 17 % 5;
|
||||||
|
let y = 10 % 3;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 2
|
||||||
|
move r9 1
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn exponentiation() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = 2 ** 8;
|
||||||
|
let y = 3 ** 3;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
pow r1 2 8
|
||||||
|
move r8 r1
|
||||||
|
pow r2 3 3
|
||||||
|
move r9 r2
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn comparison_with_zero() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = 0 == 0;
|
||||||
|
let y = 0 < 1;
|
||||||
|
let z = 0 > -1;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
seq r1 0 0
|
||||||
|
move r8 r1
|
||||||
|
slt r2 0 1
|
||||||
|
move r9 r2
|
||||||
|
sgt r3 0 -1
|
||||||
|
move r10 r3
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn boolean_negation_edge_cases() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = !0;
|
||||||
|
let y = !1;
|
||||||
|
let z = !100;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
seq r1 0 0
|
||||||
|
move r8 r1
|
||||||
|
seq r2 1 0
|
||||||
|
move r9 r2
|
||||||
|
seq r3 100 0
|
||||||
|
move r10 r3
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn function_with_many_parameters() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
fn many_params(a, b, c, d, e, f, g, h) {
|
||||||
|
return a + b + c + d + e + f + g + h;
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = many_params(1, 2, 3, 4, 5, 6, 7, 8);
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
many_params:
|
||||||
|
pop r8
|
||||||
|
pop r9
|
||||||
|
pop r10
|
||||||
|
pop r11
|
||||||
|
pop r12
|
||||||
|
pop r13
|
||||||
|
pop r14
|
||||||
|
push sp
|
||||||
|
push ra
|
||||||
|
sub r0 sp 3
|
||||||
|
get r1 db r0
|
||||||
|
add r2 r1 r14
|
||||||
|
add r3 r2 r13
|
||||||
|
add r4 r3 r12
|
||||||
|
add r5 r4 r11
|
||||||
|
add r6 r5 r10
|
||||||
|
add r7 r6 r9
|
||||||
|
add r1 r7 r8
|
||||||
|
move r15 r1
|
||||||
|
j __internal_L1
|
||||||
|
__internal_L1:
|
||||||
|
pop ra
|
||||||
|
pop sp
|
||||||
|
j ra
|
||||||
|
main:
|
||||||
|
push 1
|
||||||
|
push 2
|
||||||
|
push 3
|
||||||
|
push 4
|
||||||
|
push 5
|
||||||
|
push 6
|
||||||
|
push 7
|
||||||
|
push 8
|
||||||
|
jal many_params
|
||||||
|
move r8 r15
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tuple_declaration_with_functions() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check
|
||||||
|
r#"
|
||||||
|
device self = "db";
|
||||||
|
fn doSomething() {
|
||||||
|
return (self.Setting, self.Temperature);
|
||||||
|
}
|
||||||
|
|
||||||
|
let (setting, temperature) = doSomething();
|
||||||
|
"#
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {"
|
||||||
|
j main
|
||||||
|
doSomething:
|
||||||
|
push sp
|
||||||
|
push ra
|
||||||
|
l r1 db Setting
|
||||||
|
push r1
|
||||||
|
l r2 db Temperature
|
||||||
|
push r2
|
||||||
|
sub r0 sp 4
|
||||||
|
get r0 db r0
|
||||||
|
move r15 r0
|
||||||
|
j __internal_L1
|
||||||
|
__internal_L1:
|
||||||
|
sub r0 sp 3
|
||||||
|
get ra db r0
|
||||||
|
j ra
|
||||||
|
main:
|
||||||
|
jal doSomething
|
||||||
|
pop r9
|
||||||
|
pop r8
|
||||||
|
move sp r15
|
||||||
|
"}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tuple_from_simple_function() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
fn get_pair() {
|
||||||
|
return (1, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
let (a, b) = get_pair();
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {"
|
||||||
|
j main
|
||||||
|
get_pair:
|
||||||
|
push sp
|
||||||
|
push ra
|
||||||
|
push 1
|
||||||
|
push 2
|
||||||
|
sub r0 sp 4
|
||||||
|
get r0 db r0
|
||||||
|
move r15 r0
|
||||||
|
j __internal_L1
|
||||||
|
__internal_L1:
|
||||||
|
sub r0 sp 3
|
||||||
|
get ra db r0
|
||||||
|
j ra
|
||||||
|
main:
|
||||||
|
jal get_pair
|
||||||
|
pop r9
|
||||||
|
pop r8
|
||||||
|
move sp r15
|
||||||
|
"}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tuple_from_expression_not_function() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let (a, b) = (5 + 3, 10 * 2);
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 8
|
||||||
|
move r9 20
|
||||||
|
"}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
197
rust_compiler/libs/compiler/src/test/error_handling.rs
Normal file
197
rust_compiler/libs/compiler/src/test/error_handling.rs
Normal file
@@ -0,0 +1,197 @@
|
|||||||
|
use crate::Error;
|
||||||
|
use crate::variable_manager::Error as ScopeError;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unknown_identifier_error() {
|
||||||
|
let errors = compile! {
|
||||||
|
result "let x = unknown_var;"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(errors.len(), 1);
|
||||||
|
match &errors[0] {
|
||||||
|
Error::UnknownIdentifier(name, _) => {
|
||||||
|
assert_eq!(name.as_ref(), "unknown_var");
|
||||||
|
}
|
||||||
|
_ => panic!("Expected UnknownIdentifier error, got {:?}", errors[0]),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn duplicate_identifier_error() {
|
||||||
|
let errors = compile! {
|
||||||
|
result "
|
||||||
|
let x = 5;
|
||||||
|
let x = 10;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(errors.len(), 1);
|
||||||
|
match &errors[0] {
|
||||||
|
Error::Scope(ScopeError::DuplicateVariable(name, _)) => {
|
||||||
|
assert_eq!(name.as_ref(), "x");
|
||||||
|
}
|
||||||
|
_ => panic!("Expected DuplicateIdentifier error, got {:?}", errors[0]),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn const_reassignment_error() {
|
||||||
|
let errors = compile! {
|
||||||
|
result "
|
||||||
|
const PI = 3.14;
|
||||||
|
PI = 2.71;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(errors.len(), 1);
|
||||||
|
match &errors[0] {
|
||||||
|
Error::ConstAssignment(name, _) => {
|
||||||
|
assert_eq!(name.as_ref(), "PI");
|
||||||
|
}
|
||||||
|
_ => panic!("Expected ConstAssignment error, got {:?}", errors[0]),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unknown_function_call_error() {
|
||||||
|
let errors = compile! {
|
||||||
|
result "
|
||||||
|
let result = unknown_function();
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(errors.len(), 1);
|
||||||
|
match &errors[0] {
|
||||||
|
Error::UnknownIdentifier(name, _) => {
|
||||||
|
assert_eq!(name.as_ref(), "unknown_function");
|
||||||
|
}
|
||||||
|
_ => panic!("Expected UnknownIdentifier error, got {:?}", errors[0]),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn argument_mismatch_error() {
|
||||||
|
let errors = compile! {
|
||||||
|
result "
|
||||||
|
fn add(a, b) {
|
||||||
|
return a + b;
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = add(1);
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
// The error should be an AgrumentMismatch
|
||||||
|
assert!(
|
||||||
|
errors
|
||||||
|
.iter()
|
||||||
|
.any(|e| matches!(e, Error::AgrumentMismatch(_, _)))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tuple_size_mismatch_error() {
|
||||||
|
let errors = compile! {
|
||||||
|
result "
|
||||||
|
fn pair() {
|
||||||
|
return (1, 2);
|
||||||
|
};
|
||||||
|
|
||||||
|
let (x, y, z) = pair();
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
errors
|
||||||
|
.iter()
|
||||||
|
.any(|e| matches!(e, Error::TupleSizeMismatch(2, 3, _)))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn multiple_errors_reported() {
|
||||||
|
let errors = compile! {
|
||||||
|
result "
|
||||||
|
let x = unknown1;
|
||||||
|
let x = 5;
|
||||||
|
let y = unknown2;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Should have at least 3 errors
|
||||||
|
assert!(
|
||||||
|
errors.len() >= 2,
|
||||||
|
"Expected at least 2 errors, got {}",
|
||||||
|
errors.len()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn return_outside_function_error() {
|
||||||
|
let errors = compile! {
|
||||||
|
result "
|
||||||
|
let x = 5;
|
||||||
|
return x;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Should have an error about return outside function
|
||||||
|
assert!(
|
||||||
|
!errors.is_empty(),
|
||||||
|
"Expected error for return outside function"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn break_outside_loop_error() {
|
||||||
|
let errors = compile! {
|
||||||
|
result "
|
||||||
|
break;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(!errors.is_empty(), "Expected error for break outside loop");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn continue_outside_loop_error() {
|
||||||
|
let errors = compile! {
|
||||||
|
result "
|
||||||
|
continue;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
!errors.is_empty(),
|
||||||
|
"Expected error for continue outside loop"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn device_reassignment_error() {
|
||||||
|
let errors = compile! {
|
||||||
|
result "
|
||||||
|
device d0 = \"d0\";
|
||||||
|
device d0 = \"d1\";
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
errors
|
||||||
|
.iter()
|
||||||
|
.any(|e| matches!(e, Error::DuplicateIdentifier(_, _)))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn invalid_device_error() {
|
||||||
|
let errors = compile! {
|
||||||
|
result "
|
||||||
|
device d0 = \"d0\";
|
||||||
|
d0 = \"d1\";
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Device reassignment should fail
|
||||||
|
assert!(!errors.is_empty(), "Expected error for device reassignment");
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@ use pretty_assertions::assert_eq;
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_function_declaration_with_spillover_params() -> anyhow::Result<()> {
|
fn test_function_declaration_with_spillover_params() -> anyhow::Result<()> {
|
||||||
let compiled = compile!(debug r#"
|
let compiled = compile!(check r#"
|
||||||
// we need more than 4 params to 'spill' into a stack var
|
// we need more than 4 params to 'spill' into a stack var
|
||||||
fn doSomething(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) {
|
fn doSomething(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) {
|
||||||
return arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9;
|
return arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7 + arg8 + arg9;
|
||||||
@@ -13,8 +13,14 @@ fn test_function_declaration_with_spillover_params() -> anyhow::Result<()> {
|
|||||||
let returned = doSomething(item1, 2, 3, 4, 5, 6, 7, 8, 9);
|
let returned = doSomething(item1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||||
"#);
|
"#);
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {"
|
indoc! {"
|
||||||
j main
|
j main
|
||||||
doSomething:
|
doSomething:
|
||||||
@@ -25,10 +31,11 @@ fn test_function_declaration_with_spillover_params() -> anyhow::Result<()> {
|
|||||||
pop r12
|
pop r12
|
||||||
pop r13
|
pop r13
|
||||||
pop r14
|
pop r14
|
||||||
|
push sp
|
||||||
push ra
|
push ra
|
||||||
sub r0 sp 3
|
sub r0 sp 4
|
||||||
get r1 db r0
|
get r1 db r0
|
||||||
sub r0 sp 2
|
sub r0 sp 3
|
||||||
get r2 db r0
|
get r2 db r0
|
||||||
add r3 r1 r2
|
add r3 r1 r2
|
||||||
add r4 r3 r14
|
add r4 r3 r14
|
||||||
@@ -42,7 +49,7 @@ fn test_function_declaration_with_spillover_params() -> anyhow::Result<()> {
|
|||||||
j __internal_L1
|
j __internal_L1
|
||||||
__internal_L1:
|
__internal_L1:
|
||||||
pop ra
|
pop ra
|
||||||
sub sp sp 2
|
pop sp
|
||||||
j ra
|
j ra
|
||||||
main:
|
main:
|
||||||
move r8 1
|
move r8 1
|
||||||
@@ -67,7 +74,7 @@ fn test_function_declaration_with_spillover_params() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_early_return() -> anyhow::Result<()> {
|
fn test_early_return() -> anyhow::Result<()> {
|
||||||
let compiled = compile!(debug r#"
|
let compiled = compile!(check r#"
|
||||||
// This is a test function declaration with no body
|
// This is a test function declaration with no body
|
||||||
fn doSomething() {
|
fn doSomething() {
|
||||||
if (1 == 1) {
|
if (1 == 1) {
|
||||||
@@ -79,12 +86,19 @@ fn test_early_return() -> anyhow::Result<()> {
|
|||||||
doSomething();
|
doSomething();
|
||||||
"#);
|
"#);
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
doSomething:
|
doSomething:
|
||||||
|
push sp
|
||||||
push ra
|
push ra
|
||||||
seq r1 1 1
|
seq r1 1 1
|
||||||
beqz r1 __internal_L2
|
beqz r1 __internal_L2
|
||||||
@@ -94,6 +108,7 @@ fn test_early_return() -> anyhow::Result<()> {
|
|||||||
j __internal_L1
|
j __internal_L1
|
||||||
__internal_L1:
|
__internal_L1:
|
||||||
pop ra
|
pop ra
|
||||||
|
pop sp
|
||||||
j ra
|
j ra
|
||||||
main:
|
main:
|
||||||
jal doSomething
|
jal doSomething
|
||||||
@@ -107,22 +122,30 @@ fn test_early_return() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
#[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!(check r#"
|
||||||
// This is a test function declaration with no body
|
// This is a test function declaration with no body
|
||||||
fn doSomething(arg1, arg2) {
|
fn doSomething(arg1, arg2) {
|
||||||
};
|
};
|
||||||
"#);
|
"#);
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {"
|
indoc! {"
|
||||||
j main
|
j main
|
||||||
doSomething:
|
doSomething:
|
||||||
pop r8
|
pop r8
|
||||||
pop r9
|
pop r9
|
||||||
|
push sp
|
||||||
push ra
|
push ra
|
||||||
__internal_L1:
|
__internal_L1:
|
||||||
pop ra
|
pop ra
|
||||||
|
pop sp
|
||||||
j ra
|
j ra
|
||||||
"}
|
"}
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ use pretty_assertions::assert_eq;
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_comparison_expressions() -> anyhow::Result<()> {
|
fn test_comparison_expressions() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let isGreater = 10 > 5;
|
let isGreater = 10 > 5;
|
||||||
let isLess = 5 < 10;
|
let isLess = 5 < 10;
|
||||||
@@ -15,8 +15,14 @@ fn test_comparison_expressions() -> anyhow::Result<()> {
|
|||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -42,8 +48,8 @@ fn test_comparison_expressions() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_logical_and_or_not() -> anyhow::Result<()> {
|
fn test_logical_and_or_not() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let logic1 = 1 && 1;
|
let logic1 = 1 && 1;
|
||||||
let logic2 = 1 || 0;
|
let logic2 = 1 || 0;
|
||||||
@@ -51,8 +57,14 @@ fn test_logical_and_or_not() -> anyhow::Result<()> {
|
|||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -72,15 +84,21 @@ fn test_logical_and_or_not() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_complex_logic() -> anyhow::Result<()> {
|
fn test_complex_logic() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let logic = (10 > 5) && (5 < 10);
|
let logic = (10 > 5) && (5 < 10);
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -98,15 +116,21 @@ fn test_complex_logic() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_math_with_logic() -> anyhow::Result<()> {
|
fn test_math_with_logic() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let logic = (1 + 2) > 1;
|
let logic = (1 + 2) > 1;
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -122,15 +146,21 @@ fn test_math_with_logic() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_boolean_in_logic() -> anyhow::Result<()> {
|
fn test_boolean_in_logic() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let res = true && false;
|
let res = true && false;
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -146,8 +176,8 @@ fn test_boolean_in_logic() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_invert_a_boolean() -> anyhow::Result<()> {
|
fn test_invert_a_boolean() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let i = true;
|
let i = true;
|
||||||
let y = !i;
|
let y = !i;
|
||||||
@@ -156,8 +186,14 @@ fn test_invert_a_boolean() -> anyhow::Result<()> {
|
|||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ use pretty_assertions::assert_eq;
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_infinite_loop() -> anyhow::Result<()> {
|
fn test_infinite_loop() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let a = 0;
|
let a = 0;
|
||||||
loop {
|
loop {
|
||||||
@@ -13,9 +13,15 @@ fn test_infinite_loop() -> anyhow::Result<()> {
|
|||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
// __internal_Labels: L1 (start), L2 (end)
|
// __internal_Labels: L1 (start), L2 (end)
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -35,8 +41,8 @@ fn test_infinite_loop() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_loop_break() -> anyhow::Result<()> {
|
fn test_loop_break() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let a = 0;
|
let a = 0;
|
||||||
loop {
|
loop {
|
||||||
@@ -48,9 +54,15 @@ fn test_loop_break() -> anyhow::Result<()> {
|
|||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
// __internal_Labels: L1 (start), L2 (end), L3 (if end - implicit else label)
|
// __internal_Labels: L1 (start), L2 (end), L3 (if end - implicit else label)
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -74,8 +86,8 @@ fn test_loop_break() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_while_loop() -> anyhow::Result<()> {
|
fn test_while_loop() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let a = 0;
|
let a = 0;
|
||||||
while (a < 10) {
|
while (a < 10) {
|
||||||
@@ -84,9 +96,15 @@ fn test_while_loop() -> anyhow::Result<()> {
|
|||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
// __internal_Labels: L1 (start), L2 (end)
|
// __internal_Labels: L1 (start), L2 (end)
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -108,8 +126,8 @@ fn test_while_loop() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_loop_continue() -> anyhow::Result<()> {
|
fn test_loop_continue() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let result = compile! {
|
||||||
debug
|
check
|
||||||
r#"
|
r#"
|
||||||
let a = 0;
|
let a = 0;
|
||||||
loop {
|
loop {
|
||||||
@@ -122,9 +140,15 @@ fn test_loop_continue() -> anyhow::Result<()> {
|
|||||||
"#
|
"#
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
result.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
result.errors
|
||||||
|
);
|
||||||
|
|
||||||
// __internal_Labels: L1 (start), L2 (end), L3 (if end)
|
// __internal_Labels: L1 (start), L2 (end), L3 (if end)
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
result.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
|
|||||||
@@ -5,14 +5,20 @@ use pretty_assertions::assert_eq;
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_acos() -> Result<()> {
|
fn test_acos() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let i = acos(123);
|
let i = acos(123);
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -29,14 +35,20 @@ fn test_acos() -> Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_asin() -> Result<()> {
|
fn test_asin() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let i = asin(123);
|
let i = asin(123);
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -53,14 +65,20 @@ fn test_asin() -> Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_atan() -> Result<()> {
|
fn test_atan() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let i = atan(123);
|
let i = atan(123);
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -77,14 +95,20 @@ fn test_atan() -> Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_atan2() -> Result<()> {
|
fn test_atan2() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let i = atan2(123, 456);
|
let i = atan2(123, 456);
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -101,14 +125,20 @@ fn test_atan2() -> Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_abs() -> Result<()> {
|
fn test_abs() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let i = abs(-123);
|
let i = abs(-123);
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -125,14 +155,20 @@ fn test_abs() -> Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_ceil() -> Result<()> {
|
fn test_ceil() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let i = ceil(123.90);
|
let i = ceil(123.90);
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -149,14 +185,20 @@ fn test_ceil() -> Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_cos() -> Result<()> {
|
fn test_cos() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let i = cos(123);
|
let i = cos(123);
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -173,14 +215,20 @@ fn test_cos() -> Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_floor() -> Result<()> {
|
fn test_floor() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let i = floor(123);
|
let i = floor(123);
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -197,14 +245,20 @@ fn test_floor() -> Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_log() -> Result<()> {
|
fn test_log() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let i = log(123);
|
let i = log(123);
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -221,14 +275,20 @@ fn test_log() -> Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_max() -> Result<()> {
|
fn test_max() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let i = max(123, 456);
|
let i = max(123, 456);
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -245,15 +305,21 @@ fn test_max() -> Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_max_from_game() -> Result<()> {
|
fn test_max_from_game() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
r#"
|
r#"
|
||||||
let item = 0;
|
let item = 0;
|
||||||
item = max(1 + 2, 2);
|
item = max(1 + 2, 2);
|
||||||
"#
|
"#
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -271,14 +337,20 @@ fn test_max_from_game() -> Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_min() -> Result<()> {
|
fn test_min() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let i = min(123, 456);
|
let i = min(123, 456);
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -295,14 +367,20 @@ fn test_min() -> Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_rand() -> Result<()> {
|
fn test_rand() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let i = rand();
|
let i = rand();
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -319,14 +397,20 @@ fn test_rand() -> Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_sin() -> Result<()> {
|
fn test_sin() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let i = sin(3);
|
let i = sin(3);
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -343,14 +427,20 @@ fn test_sin() -> Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_sqrt() -> Result<()> {
|
fn test_sqrt() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let i = sqrt(3);
|
let i = sqrt(3);
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -367,14 +457,20 @@ fn test_sqrt() -> Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_tan() -> Result<()> {
|
fn test_tan() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let i = tan(3);
|
let i = tan(3);
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -391,14 +487,20 @@ fn test_tan() -> Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_trunc() -> Result<()> {
|
fn test_trunc() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
let i = trunc(3.234);
|
let i = trunc(3.234);
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
|
|||||||
@@ -6,6 +6,12 @@ macro_rules! output {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents both compilation errors and compiled output
|
||||||
|
pub struct CompilationCheckResult {
|
||||||
|
pub errors: Vec<crate::Error<'static>>,
|
||||||
|
pub output: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg_attr(test, macro_export)]
|
#[cfg_attr(test, macro_export)]
|
||||||
macro_rules! compile {
|
macro_rules! compile {
|
||||||
($source:expr) => {{
|
($source:expr) => {{
|
||||||
@@ -27,7 +33,7 @@ macro_rules! compile {
|
|||||||
compiler.compile().errors
|
compiler.compile().errors
|
||||||
}};
|
}};
|
||||||
|
|
||||||
(debug $source:expr) => {{
|
(check $source:expr) => {{
|
||||||
let mut writer = std::io::BufWriter::new(Vec::new());
|
let mut writer = std::io::BufWriter::new(Vec::new());
|
||||||
let compiler = crate::Compiler::new(
|
let compiler = crate::Compiler::new(
|
||||||
parser::Parser::new(tokenizer::Tokenizer::from($source)),
|
parser::Parser::new(tokenizer::Tokenizer::from($source)),
|
||||||
@@ -35,15 +41,25 @@ macro_rules! compile {
|
|||||||
);
|
);
|
||||||
let res = compiler.compile();
|
let res = compiler.compile();
|
||||||
res.instructions.write(&mut writer)?;
|
res.instructions.write(&mut writer)?;
|
||||||
output!(writer)
|
let output = output!(writer);
|
||||||
|
crate::test::CompilationCheckResult {
|
||||||
|
errors: res.errors,
|
||||||
|
output,
|
||||||
|
}
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
mod binary_expression;
|
mod binary_expression;
|
||||||
mod branching;
|
mod branching;
|
||||||
mod declaration_function_invocation;
|
mod declaration_function_invocation;
|
||||||
mod declaration_literal;
|
mod declaration_literal;
|
||||||
|
mod device_access;
|
||||||
|
mod edge_cases;
|
||||||
|
mod error_handling;
|
||||||
mod function_declaration;
|
mod function_declaration;
|
||||||
mod logic_expression;
|
mod logic_expression;
|
||||||
mod loops;
|
mod loops;
|
||||||
mod math_syscall;
|
mod math_syscall;
|
||||||
|
mod negation_priority;
|
||||||
|
mod scoping;
|
||||||
mod syscall;
|
mod syscall;
|
||||||
|
mod tuple_literals;
|
||||||
|
|||||||
388
rust_compiler/libs/compiler/src/test/negation_priority.rs
Normal file
388
rust_compiler/libs/compiler/src/test/negation_priority.rs
Normal file
@@ -0,0 +1,388 @@
|
|||||||
|
use indoc::indoc;
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn simple_negation() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = -5;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 -5
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn negation_of_variable() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = 10;
|
||||||
|
let y = -x;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 10
|
||||||
|
sub r1 0 r8
|
||||||
|
move r9 r1
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn double_negation() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = -(-5);
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 5
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn negation_in_expression() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = 10 + (-5);
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 5
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn negation_with_multiplication() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = -3 * 4;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 -12
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parentheses_priority() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = (2 + 3) * 4;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 20
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn nested_parentheses() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = ((2 + 3) * (4 - 1));
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 15
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parentheses_with_variables() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let a = 5;
|
||||||
|
let b = 10;
|
||||||
|
let c = (a + b) * 2;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
// Should calculate (5 + 10) * 2 = 30
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 5
|
||||||
|
move r9 10
|
||||||
|
add r1 r8 r9
|
||||||
|
mul r2 r1 2
|
||||||
|
move r10 r2
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn priority_affects_result() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let with_priority = (2 + 3) * 4;
|
||||||
|
let without_priority = 2 + 3 * 4;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
// with_priority should be 20, without_priority should be 14
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 20
|
||||||
|
move r9 14
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn negation_of_expression() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = -(2 + 3);
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
// Should be -5 (constant folded)
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
sub r1 0 5
|
||||||
|
move r8 r1
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn complex_negation_and_priority() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = -((10 - 5) * 2);
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
// Should be -(5 * 2) = -10 (folded to constant)
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
sub r1 0 10
|
||||||
|
move r8 r1
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn negation_in_logical_expression() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = !(-5);
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
// -5 is truthy, so !(-5) should be 0
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
sub r1 0 5
|
||||||
|
seq r2 r1 0
|
||||||
|
move r8 r2
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parentheses_in_comparison() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = (10 + 5) > (3 * 4);
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
// (10 + 5) = 15 > (3 * 4) = 12, so true (1)
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
sgt r1 15 12
|
||||||
|
move r8 r1
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
462
rust_compiler/libs/compiler/src/test/scoping.rs
Normal file
462
rust_compiler/libs/compiler/src/test/scoping.rs
Normal file
@@ -0,0 +1,462 @@
|
|||||||
|
use indoc::indoc;
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn block_scope() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = 10;
|
||||||
|
{
|
||||||
|
let y = 20;
|
||||||
|
let z = x + y;
|
||||||
|
}
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 10
|
||||||
|
move r9 20
|
||||||
|
add r1 r8 r9
|
||||||
|
move r10 r1
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn variable_scope_isolation() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = 10;
|
||||||
|
{
|
||||||
|
let x = 20;
|
||||||
|
let y = x;
|
||||||
|
}
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 10
|
||||||
|
move r9 20
|
||||||
|
move r10 r9
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn function_parameter_scope() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
fn double(x) {
|
||||||
|
return x * 2;
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = double(5);
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
double:
|
||||||
|
pop r8
|
||||||
|
push sp
|
||||||
|
push ra
|
||||||
|
mul r1 r8 2
|
||||||
|
move r15 r1
|
||||||
|
j __internal_L1
|
||||||
|
__internal_L1:
|
||||||
|
pop ra
|
||||||
|
pop sp
|
||||||
|
j ra
|
||||||
|
main:
|
||||||
|
push 5
|
||||||
|
jal double
|
||||||
|
move r8 r15
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn nested_block_scopes() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = 1;
|
||||||
|
{
|
||||||
|
let x = 2;
|
||||||
|
{
|
||||||
|
let x = 3;
|
||||||
|
let y = x;
|
||||||
|
}
|
||||||
|
let z = x;
|
||||||
|
}
|
||||||
|
let w = x;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 1
|
||||||
|
move r9 2
|
||||||
|
move r10 3
|
||||||
|
move r11 r10
|
||||||
|
move r10 r9
|
||||||
|
move r9 r8
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn variable_shadowing_in_conditional() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = 10;
|
||||||
|
|
||||||
|
if (true) {
|
||||||
|
let x = 20;
|
||||||
|
}
|
||||||
|
|
||||||
|
let y = x;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 10
|
||||||
|
beqz 1 __internal_L1
|
||||||
|
move r9 20
|
||||||
|
__internal_L1:
|
||||||
|
move r9 r8
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn variable_shadowing_in_loop() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = 0;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let x = x + 1;
|
||||||
|
if (x > 5) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 0
|
||||||
|
__internal_L1:
|
||||||
|
add r1 r8 1
|
||||||
|
move r9 r1
|
||||||
|
sgt r2 r9 5
|
||||||
|
beqz r2 __internal_L3
|
||||||
|
j __internal_L2
|
||||||
|
__internal_L3:
|
||||||
|
j __internal_L1
|
||||||
|
__internal_L2:
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn const_scope() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
const PI = 3.14;
|
||||||
|
|
||||||
|
{
|
||||||
|
const PI = 2.71;
|
||||||
|
let x = PI;
|
||||||
|
}
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 2.71
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn device_in_scope() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
device d0 = \"d0\";
|
||||||
|
|
||||||
|
{
|
||||||
|
let value = d0.Temperature;
|
||||||
|
}
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
l r1 d0 Temperature
|
||||||
|
move r8 r1
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn function_scope_isolation() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
fn func1() {
|
||||||
|
let x = 10;
|
||||||
|
return x;
|
||||||
|
};
|
||||||
|
|
||||||
|
fn func2() {
|
||||||
|
let x = 20;
|
||||||
|
return x;
|
||||||
|
};
|
||||||
|
|
||||||
|
let a = func1();
|
||||||
|
let b = func2();
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
func1:
|
||||||
|
push sp
|
||||||
|
push ra
|
||||||
|
move r8 10
|
||||||
|
move r15 r8
|
||||||
|
j __internal_L1
|
||||||
|
__internal_L1:
|
||||||
|
pop ra
|
||||||
|
pop sp
|
||||||
|
j ra
|
||||||
|
func2:
|
||||||
|
push sp
|
||||||
|
push ra
|
||||||
|
move r8 20
|
||||||
|
move r15 r8
|
||||||
|
j __internal_L2
|
||||||
|
__internal_L2:
|
||||||
|
pop ra
|
||||||
|
pop sp
|
||||||
|
j ra
|
||||||
|
main:
|
||||||
|
jal func1
|
||||||
|
move r8 r15
|
||||||
|
push r8
|
||||||
|
jal func2
|
||||||
|
pop r8
|
||||||
|
move r9 r15
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tuple_unpacking_scope() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
fn pair() {
|
||||||
|
return (1, 2);
|
||||||
|
};
|
||||||
|
|
||||||
|
{
|
||||||
|
let (x, y) = pair();
|
||||||
|
let z = x + y;
|
||||||
|
}
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
pair:
|
||||||
|
push sp
|
||||||
|
push ra
|
||||||
|
push 1
|
||||||
|
push 2
|
||||||
|
sub r0 sp 4
|
||||||
|
get r0 db r0
|
||||||
|
move r15 r0
|
||||||
|
j __internal_L1
|
||||||
|
__internal_L1:
|
||||||
|
sub r0 sp 3
|
||||||
|
get ra db r0
|
||||||
|
j ra
|
||||||
|
main:
|
||||||
|
jal pair
|
||||||
|
pop r9
|
||||||
|
pop r8
|
||||||
|
move sp r15
|
||||||
|
add r1 r8 r9
|
||||||
|
move r10 r1
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn shadowing_doesnt_affect_outer() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
check "
|
||||||
|
let x = 5;
|
||||||
|
let y = x;
|
||||||
|
{
|
||||||
|
let x = 10;
|
||||||
|
let z = x;
|
||||||
|
}
|
||||||
|
let w = x + y;
|
||||||
|
"
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled.output,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 5
|
||||||
|
move r9 r8
|
||||||
|
move r10 10
|
||||||
|
move r11 r10
|
||||||
|
add r1 r8 r9
|
||||||
|
move r10 r1
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -4,14 +4,20 @@ use pretty_assertions::assert_eq;
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_yield() -> anyhow::Result<()> {
|
fn test_yield() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
yield();
|
yield();
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -27,7 +33,7 @@ fn test_yield() -> anyhow::Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_sleep() -> anyhow::Result<()> {
|
fn test_sleep() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
"
|
"
|
||||||
sleep(3);
|
sleep(3);
|
||||||
let sleepAmount = 15;
|
let sleepAmount = 15;
|
||||||
@@ -36,8 +42,14 @@ fn test_sleep() -> anyhow::Result<()> {
|
|||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -57,7 +69,7 @@ fn test_sleep() -> anyhow::Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_set_on_device() -> anyhow::Result<()> {
|
fn test_set_on_device() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
r#"
|
r#"
|
||||||
device airConditioner = "d0";
|
device airConditioner = "d0";
|
||||||
let internalTemp = 20c;
|
let internalTemp = 20c;
|
||||||
@@ -66,8 +78,14 @@ fn test_set_on_device() -> anyhow::Result<()> {
|
|||||||
"#
|
"#
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -85,15 +103,21 @@ fn test_set_on_device() -> anyhow::Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_set_on_device_batched() -> anyhow::Result<()> {
|
fn test_set_on_device_batched() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
r#"
|
r#"
|
||||||
const doorHash = hash("Door");
|
const doorHash = hash("Door");
|
||||||
setBatched(doorHash, "Lock", true);
|
setBatched(doorHash, "Lock", true);
|
||||||
"#
|
"#
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
r#"
|
r#"
|
||||||
j main
|
j main
|
||||||
@@ -108,7 +132,7 @@ fn test_set_on_device_batched() -> anyhow::Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_set_on_device_batched_named() -> anyhow::Result<()> {
|
fn test_set_on_device_batched_named() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
r#"
|
r#"
|
||||||
device dev = "d0";
|
device dev = "d0";
|
||||||
const devName = hash("test");
|
const devName = hash("test");
|
||||||
@@ -117,8 +141,14 @@ fn test_set_on_device_batched_named() -> anyhow::Result<()> {
|
|||||||
"#
|
"#
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -134,7 +164,7 @@ fn test_set_on_device_batched_named() -> anyhow::Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_load_from_device() -> anyhow::Result<()> {
|
fn test_load_from_device() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
r#"
|
r#"
|
||||||
device airCon = "d0";
|
device airCon = "d0";
|
||||||
|
|
||||||
@@ -142,8 +172,14 @@ fn test_load_from_device() -> anyhow::Result<()> {
|
|||||||
"#
|
"#
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -160,7 +196,7 @@ fn test_load_from_device() -> anyhow::Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_load_from_slot() -> anyhow::Result<()> {
|
fn test_load_from_slot() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
r#"
|
r#"
|
||||||
device airCon = "d0";
|
device airCon = "d0";
|
||||||
|
|
||||||
@@ -168,8 +204,14 @@ fn test_load_from_slot() -> anyhow::Result<()> {
|
|||||||
"#
|
"#
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -186,7 +228,7 @@ fn test_load_from_slot() -> anyhow::Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_set_slot() -> anyhow::Result<()> {
|
fn test_set_slot() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
r#"
|
r#"
|
||||||
device airCon = "d0";
|
device airCon = "d0";
|
||||||
|
|
||||||
@@ -194,8 +236,14 @@ fn test_set_slot() -> anyhow::Result<()> {
|
|||||||
"#
|
"#
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
@@ -211,7 +259,7 @@ fn test_set_slot() -> anyhow::Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_load_reagent() -> anyhow::Result<()> {
|
fn test_load_reagent() -> anyhow::Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
check
|
||||||
r#"
|
r#"
|
||||||
device thingy = "d0";
|
device thingy = "d0";
|
||||||
|
|
||||||
@@ -219,8 +267,14 @@ fn test_load_reagent() -> anyhow::Result<()> {
|
|||||||
"#
|
"#
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
compiled.errors.is_empty(),
|
||||||
|
"Expected no errors, got: {:?}",
|
||||||
|
compiled.errors
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
compiled,
|
compiled.output,
|
||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
|
|||||||
1291
rust_compiler/libs/compiler/src/test/tuple_literals.rs
Normal file
1291
rust_compiler/libs/compiler/src/test/tuple_literals.rs
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -441,7 +441,13 @@ impl<'a> Parser<'a> {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
TokenType::Keyword(Keyword::Let) => Some(self.spanned(|p| p.declaration())?),
|
TokenType::Keyword(Keyword::Let) => {
|
||||||
|
if self_matches_peek!(self, TokenType::Symbol(Symbol::LParen)) {
|
||||||
|
Some(self.spanned(|p| p.tuple_declaration())?)
|
||||||
|
} else {
|
||||||
|
Some(self.spanned(|p| p.declaration())?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TokenType::Keyword(Keyword::Device) => {
|
TokenType::Keyword(Keyword::Device) => {
|
||||||
let spanned_dev = self.spanned(|p| p.device())?;
|
let spanned_dev = self.spanned(|p| p.device())?;
|
||||||
@@ -561,9 +567,7 @@ impl<'a> Parser<'a> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
TokenType::Symbol(Symbol::LParen) => {
|
TokenType::Symbol(Symbol::LParen) => self.parenthesized_or_tuple()?,
|
||||||
self.spanned(|p| p.priority())?.node.map(|node| *node)
|
|
||||||
}
|
|
||||||
|
|
||||||
TokenType::Symbol(Symbol::Minus) => {
|
TokenType::Symbol(Symbol::Minus) => {
|
||||||
let start_span = self.current_span();
|
let start_span = self.current_span();
|
||||||
@@ -642,8 +646,8 @@ impl<'a> Parser<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
TokenType::Symbol(Symbol::LParen) => *self
|
TokenType::Symbol(Symbol::LParen) => *self
|
||||||
.spanned(|p| p.priority())?
|
.parenthesized_or_tuple()?
|
||||||
.node
|
.map(Box::new)
|
||||||
.ok_or(Error::UnexpectedEOF)?,
|
.ok_or(Error::UnexpectedEOF)?,
|
||||||
|
|
||||||
TokenType::Identifier(ref id) if SysCall::is_syscall(id) => {
|
TokenType::Identifier(ref id) if SysCall::is_syscall(id) => {
|
||||||
@@ -774,7 +778,8 @@ impl<'a> Parser<'a> {
|
|||||||
| Expression::Ternary(_)
|
| Expression::Ternary(_)
|
||||||
| Expression::Negation(_)
|
| Expression::Negation(_)
|
||||||
| Expression::MemberAccess(_)
|
| Expression::MemberAccess(_)
|
||||||
| Expression::MethodCall(_) => {}
|
| Expression::MethodCall(_)
|
||||||
|
| Expression::Tuple(_) => {}
|
||||||
_ => {
|
_ => {
|
||||||
return Err(Error::InvalidSyntax(
|
return Err(Error::InvalidSyntax(
|
||||||
self.current_span(),
|
self.current_span(),
|
||||||
@@ -1081,19 +1086,39 @@ impl<'a> Parser<'a> {
|
|||||||
end_col: right.span.end_col,
|
end_col: right.span.end_col,
|
||||||
};
|
};
|
||||||
|
|
||||||
expressions.insert(
|
// Check if the left side is a tuple, and if so, create a TupleAssignment
|
||||||
i,
|
let node = if let Expression::Tuple(tuple_expr) = &left.node {
|
||||||
Spanned {
|
// Extract variable names from the tuple, handling underscores
|
||||||
|
let mut names = Vec::new();
|
||||||
|
for item in &tuple_expr.node {
|
||||||
|
if let Expression::Variable(var) = &item.node {
|
||||||
|
names.push(var.clone());
|
||||||
|
} else {
|
||||||
|
return Err(Error::InvalidSyntax(
|
||||||
|
item.span,
|
||||||
|
String::from("Tuple assignment can only contain variable names"),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Expression::TupleAssignment(Spanned {
|
||||||
span,
|
span,
|
||||||
node: Expression::Assignment(Spanned {
|
node: TupleAssignmentExpression {
|
||||||
|
names,
|
||||||
|
value: boxed!(right),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Expression::Assignment(Spanned {
|
||||||
span,
|
span,
|
||||||
node: AssignmentExpression {
|
node: AssignmentExpression {
|
||||||
assignee: boxed!(left),
|
assignee: boxed!(left),
|
||||||
expression: boxed!(right),
|
expression: boxed!(right),
|
||||||
},
|
},
|
||||||
}),
|
})
|
||||||
},
|
};
|
||||||
);
|
|
||||||
|
expressions.insert(i, Spanned { span, node });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
operators.retain(|symbol| !matches!(symbol, Symbol::Assign));
|
operators.retain(|symbol| !matches!(symbol, Symbol::Assign));
|
||||||
@@ -1117,8 +1142,12 @@ impl<'a> Parser<'a> {
|
|||||||
expressions.pop().ok_or(Error::UnexpectedEOF)
|
expressions.pop().ok_or(Error::UnexpectedEOF)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn priority(&mut self) -> Result<Option<Box<Spanned<Expression<'a>>>>, Error<'a>> {
|
fn parenthesized_or_tuple(
|
||||||
|
&mut self,
|
||||||
|
) -> Result<Option<Spanned<tree_node::Expression<'a>>>, Error<'a>> {
|
||||||
|
let start_span = self.current_span();
|
||||||
let current_token = self.current_token.as_ref().ok_or(Error::UnexpectedEOF)?;
|
let current_token = self.current_token.as_ref().ok_or(Error::UnexpectedEOF)?;
|
||||||
|
|
||||||
if !token_matches!(current_token, TokenType::Symbol(Symbol::LParen)) {
|
if !token_matches!(current_token, TokenType::Symbol(Symbol::LParen)) {
|
||||||
return Err(Error::UnexpectedToken(
|
return Err(Error::UnexpectedToken(
|
||||||
self.current_span(),
|
self.current_span(),
|
||||||
@@ -1127,17 +1156,112 @@ impl<'a> Parser<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.assign_next()?;
|
self.assign_next()?;
|
||||||
let expression = self.expression()?.ok_or(Error::UnexpectedEOF)?;
|
|
||||||
|
|
||||||
let current_token = self.get_next()?.ok_or(Error::UnexpectedEOF)?;
|
// Handle empty tuple '()'
|
||||||
if !token_matches!(current_token, TokenType::Symbol(Symbol::RParen)) {
|
if self_matches_peek!(self, TokenType::Symbol(Symbol::RParen)) {
|
||||||
return Err(Error::UnexpectedToken(
|
self.assign_next()?;
|
||||||
Self::token_to_span(¤t_token),
|
let end_span = self.current_span();
|
||||||
current_token,
|
let span = Span {
|
||||||
));
|
start_line: start_span.start_line,
|
||||||
|
start_col: start_span.start_col,
|
||||||
|
end_line: end_span.end_line,
|
||||||
|
end_col: end_span.end_col,
|
||||||
|
};
|
||||||
|
return Ok(Some(Spanned {
|
||||||
|
span,
|
||||||
|
node: Expression::Tuple(Spanned { span, node: vec![] }),
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Some(boxed!(expression)))
|
let first_expression = self.expression()?.ok_or(Error::UnexpectedEOF)?;
|
||||||
|
|
||||||
|
if self_matches_peek!(self, TokenType::Symbol(Symbol::Comma)) {
|
||||||
|
// It is a tuple
|
||||||
|
let mut items = vec![first_expression];
|
||||||
|
while self_matches_peek!(self, TokenType::Symbol(Symbol::Comma)) {
|
||||||
|
// Next toekn is a comma, we need to consume it and advance 1 more time.
|
||||||
|
self.assign_next()?;
|
||||||
|
self.assign_next()?;
|
||||||
|
items.push(self.expression()?.ok_or(Error::UnexpectedEOF)?);
|
||||||
|
}
|
||||||
|
|
||||||
|
let next = self.get_next()?.ok_or(Error::UnexpectedEOF)?;
|
||||||
|
if !token_matches!(next, TokenType::Symbol(Symbol::RParen)) {
|
||||||
|
return Err(Error::UnexpectedToken(Self::token_to_span(&next), next));
|
||||||
|
}
|
||||||
|
|
||||||
|
let end_span = Self::token_to_span(&next);
|
||||||
|
let span = Span {
|
||||||
|
start_line: start_span.start_line,
|
||||||
|
start_col: start_span.start_col,
|
||||||
|
end_line: end_span.end_line,
|
||||||
|
end_col: end_span.end_col,
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Some(Spanned {
|
||||||
|
span,
|
||||||
|
node: Expression::Tuple(Spanned { span, node: items }),
|
||||||
|
}))
|
||||||
|
} else {
|
||||||
|
// It is just priority
|
||||||
|
let next = self.get_next()?.ok_or(Error::UnexpectedEOF)?;
|
||||||
|
if !token_matches!(next, TokenType::Symbol(Symbol::RParen)) {
|
||||||
|
return Err(Error::UnexpectedToken(Self::token_to_span(&next), next));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Some(Spanned {
|
||||||
|
span: first_expression.span,
|
||||||
|
node: Expression::Priority(boxed!(first_expression)),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tuple_declaration(&mut self) -> Result<Expression<'a>, Error<'a>> {
|
||||||
|
// 'let' is consumed before this call
|
||||||
|
// expect '('
|
||||||
|
let next = self.get_next()?.ok_or(Error::UnexpectedEOF)?;
|
||||||
|
if !token_matches!(next, TokenType::Symbol(Symbol::LParen)) {
|
||||||
|
return Err(Error::UnexpectedToken(Self::token_to_span(&next), next));
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut names = Vec::new();
|
||||||
|
while !self_matches_peek!(self, TokenType::Symbol(Symbol::RParen)) {
|
||||||
|
let token = self.get_next()?.ok_or(Error::UnexpectedEOF)?;
|
||||||
|
let span = Self::token_to_span(&token);
|
||||||
|
if let TokenType::Identifier(id) = token.token_type {
|
||||||
|
names.push(Spanned { span, node: id });
|
||||||
|
} else {
|
||||||
|
return Err(Error::UnexpectedToken(span, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
if self_matches_peek!(self, TokenType::Symbol(Symbol::Comma)) {
|
||||||
|
self.assign_next()?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.assign_next()?; // consume ')'
|
||||||
|
|
||||||
|
let assign = self.get_next()?.ok_or(Error::UnexpectedEOF)?;
|
||||||
|
|
||||||
|
if !token_matches!(assign, TokenType::Symbol(Symbol::Assign)) {
|
||||||
|
return Err(Error::UnexpectedToken(Self::token_to_span(&assign), assign));
|
||||||
|
}
|
||||||
|
|
||||||
|
self.assign_next()?; // Consume the `=`
|
||||||
|
|
||||||
|
let value = self.expression()?.ok_or(Error::UnexpectedEOF)?;
|
||||||
|
|
||||||
|
let semi = self.get_next()?.ok_or(Error::UnexpectedEOF)?;
|
||||||
|
if !token_matches!(semi, TokenType::Symbol(Symbol::Semicolon)) {
|
||||||
|
return Err(Error::UnexpectedToken(Self::token_to_span(&semi), semi));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Expression::TupleDeclaration(Spanned {
|
||||||
|
span: names.first().map(|n| n.span).unwrap_or(value.span),
|
||||||
|
node: TupleDeclarationExpression {
|
||||||
|
names,
|
||||||
|
value: boxed!(value),
|
||||||
|
},
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn invocation(&mut self) -> Result<InvocationExpression<'a>, Error<'a>> {
|
fn invocation(&mut self) -> Result<InvocationExpression<'a>, Error<'a>> {
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ fn test_function_invocation() -> Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_priority_expression() -> Result<()> {
|
fn test_priority_expression() -> Result<()> {
|
||||||
let input = r#"
|
let input = r#"
|
||||||
let x = (4);
|
let x = (4 + 3);
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
let tokenizer = Tokenizer::from(input);
|
let tokenizer = Tokenizer::from(input);
|
||||||
@@ -120,7 +120,7 @@ fn test_priority_expression() -> Result<()> {
|
|||||||
|
|
||||||
let expression = parser.parse()?.unwrap();
|
let expression = parser.parse()?.unwrap();
|
||||||
|
|
||||||
assert_eq!("(let x = 4)", expression.to_string());
|
assert_eq!("(let x = ((4 + 3)))", expression.to_string());
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -137,7 +137,7 @@ fn test_binary_expression() -> Result<()> {
|
|||||||
assert_eq!("(((45 * 2) - (15 / 5)) + (5 ** 2))", expr.to_string());
|
assert_eq!("(((45 * 2) - (15 / 5)) + (5 ** 2))", expr.to_string());
|
||||||
|
|
||||||
let expr = parser!("(5 - 2) * 10;").parse()?.unwrap();
|
let expr = parser!("(5 - 2) * 10;").parse()?.unwrap();
|
||||||
assert_eq!("((5 - 2) * 10)", expr.to_string());
|
assert_eq!("(((5 - 2)) * 10)", expr.to_string());
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -170,7 +170,7 @@ fn test_ternary_expression() -> Result<()> {
|
|||||||
fn test_complex_binary_with_ternary() -> Result<()> {
|
fn test_complex_binary_with_ternary() -> Result<()> {
|
||||||
let expr = parser!("let i = (x ? 1 : 3) * 2;").parse()?.unwrap();
|
let expr = parser!("let i = (x ? 1 : 3) * 2;").parse()?.unwrap();
|
||||||
|
|
||||||
assert_eq!("(let i = ((x ? 1 : 3) * 2))", expr.to_string());
|
assert_eq!("(let i = (((x ? 1 : 3)) * 2))", expr.to_string());
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -191,3 +191,99 @@ fn test_nested_ternary_right_associativity() -> Result<()> {
|
|||||||
assert_eq!("(let i = (a ? b : (c ? d : e)))", expr.to_string());
|
assert_eq!("(let i = (a ? b : (c ? d : e)))", expr.to_string());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_declaration() -> Result<()> {
|
||||||
|
let expr = parser!("let (x, _) = (1, 2);").parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("(let (x, _) = (1, 2))", expr.to_string());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_assignment() -> Result<()> {
|
||||||
|
let expr = parser!("(x, y) = (1, 2);").parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("((x, y) = (1, 2))", expr.to_string());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_assignment_with_underscore() -> Result<()> {
|
||||||
|
let expr = parser!("(x, _) = (1, 2);").parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("((x, _) = (1, 2))", expr.to_string());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_declaration_with_function_call() -> Result<()> {
|
||||||
|
let expr = parser!("let (x, y) = doSomething();").parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("(let (x, y) = doSomething())", expr.to_string());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_declaration_with_function_call_with_underscore() -> Result<()> {
|
||||||
|
let expr = parser!("let (x, _) = doSomething();").parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("(let (x, _) = doSomething())", expr.to_string());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_assignment_with_function_call() -> Result<()> {
|
||||||
|
let expr = parser!("(x, y) = doSomething();").parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("((x, y) = doSomething())", expr.to_string());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_assignment_with_function_call_with_underscore() -> Result<()> {
|
||||||
|
let expr = parser!("(x, _) = doSomething();").parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("((x, _) = doSomething())", expr.to_string());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_declaration_with_complex_expressions() -> Result<()> {
|
||||||
|
let expr = parser!("let (x, y) = (1 + 1, doSomething());")
|
||||||
|
.parse()?
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("(let (x, y) = ((1 + 1), doSomething()))", expr.to_string());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_assignment_with_complex_expressions() -> Result<()> {
|
||||||
|
let expr = parser!("(x, y) = (doSomething(), 123 / someValue.Setting);")
|
||||||
|
.parse()?
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
"((x, y) = (doSomething(), (123 / someValue.Setting)))",
|
||||||
|
expr.to_string()
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple_declaration_all_complex_expressions() -> Result<()> {
|
||||||
|
let expr = parser!("let (x, y) = (a + b, c * d);").parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("(let (x, y) = ((a + b), (c * d)))", expr.to_string());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
@@ -245,6 +245,42 @@ impl<'a> std::fmt::Display for DeviceDeclarationExpression<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub struct TupleDeclarationExpression<'a> {
|
||||||
|
pub names: Vec<Spanned<Cow<'a, str>>>,
|
||||||
|
pub value: Box<Spanned<Expression<'a>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> std::fmt::Display for TupleDeclarationExpression<'a> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
let names = self
|
||||||
|
.names
|
||||||
|
.iter()
|
||||||
|
.map(|n| n.node.to_string())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ");
|
||||||
|
write!(f, "(let ({}) = {})", names, self.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub struct TupleAssignmentExpression<'a> {
|
||||||
|
pub names: Vec<Spanned<Cow<'a, str>>>,
|
||||||
|
pub value: Box<Spanned<Expression<'a>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> std::fmt::Display for TupleAssignmentExpression<'a> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
let names = self
|
||||||
|
.names
|
||||||
|
.iter()
|
||||||
|
.map(|n| n.node.to_string())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ");
|
||||||
|
write!(f, "(({}) = {})", names, self.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct IfExpression<'a> {
|
pub struct IfExpression<'a> {
|
||||||
pub condition: Box<Spanned<Expression<'a>>>,
|
pub condition: Box<Spanned<Expression<'a>>>,
|
||||||
@@ -348,6 +384,9 @@ pub enum Expression<'a> {
|
|||||||
Return(Option<Box<Spanned<Expression<'a>>>>),
|
Return(Option<Box<Spanned<Expression<'a>>>>),
|
||||||
Syscall(Spanned<SysCall<'a>>),
|
Syscall(Spanned<SysCall<'a>>),
|
||||||
Ternary(Spanned<TernaryExpression<'a>>),
|
Ternary(Spanned<TernaryExpression<'a>>),
|
||||||
|
Tuple(Spanned<Vec<Spanned<Expression<'a>>>>),
|
||||||
|
TupleAssignment(Spanned<TupleAssignmentExpression<'a>>),
|
||||||
|
TupleDeclaration(Spanned<TupleDeclarationExpression<'a>>),
|
||||||
Variable(Spanned<Cow<'a, str>>),
|
Variable(Spanned<Cow<'a, str>>),
|
||||||
While(Spanned<WhileExpression<'a>>),
|
While(Spanned<WhileExpression<'a>>),
|
||||||
}
|
}
|
||||||
@@ -384,8 +423,20 @@ impl<'a> std::fmt::Display for Expression<'a> {
|
|||||||
),
|
),
|
||||||
Expression::Syscall(e) => write!(f, "{}", e),
|
Expression::Syscall(e) => write!(f, "{}", e),
|
||||||
Expression::Ternary(e) => write!(f, "{}", e),
|
Expression::Ternary(e) => write!(f, "{}", e),
|
||||||
|
Expression::Tuple(e) => {
|
||||||
|
let items = e
|
||||||
|
.node
|
||||||
|
.iter()
|
||||||
|
.map(|x| x.to_string())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ");
|
||||||
|
write!(f, "({})", items)
|
||||||
|
}
|
||||||
|
Expression::TupleAssignment(e) => write!(f, "{}", e),
|
||||||
|
Expression::TupleDeclaration(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),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -115,7 +115,7 @@ macro_rules! keyword {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Hash, Eq, Clone, Logos)]
|
#[derive(Debug, PartialEq, Hash, Eq, Clone, Logos)]
|
||||||
#[logos(skip r"[ \t\f]+")]
|
#[logos(skip r"[ \r\t\f]+")]
|
||||||
#[logos(extras = Extras)]
|
#[logos(extras = Extras)]
|
||||||
#[logos(error(LexError, LexError::from_lexer))]
|
#[logos(error(LexError, LexError::from_lexer))]
|
||||||
pub enum TokenType<'a> {
|
pub enum TokenType<'a> {
|
||||||
@@ -843,3 +843,20 @@ documented! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::TokenType;
|
||||||
|
use logos::Logos;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_windows_crlf_endings() -> anyhow::Result<()> {
|
||||||
|
let src = "let i = 0;\r\n";
|
||||||
|
|
||||||
|
let lexer = TokenType::lexer(src);
|
||||||
|
|
||||||
|
let tokens = lexer.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
assert!(!tokens.iter().any(|res| res.is_err()));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,43 +0,0 @@
|
|||||||
device self = "db";
|
|
||||||
device gasSensor = "d0";
|
|
||||||
device atmosAnal = "d1";
|
|
||||||
device atmosValve = "d2";
|
|
||||||
device atmosTank = "d3";
|
|
||||||
device atmosInlet = "d4";
|
|
||||||
|
|
||||||
atmosInlet.Lock = true;
|
|
||||||
atmosInlet.Mode = 1;
|
|
||||||
atmosValve.On = false;
|
|
||||||
atmosValve.Lock = true;
|
|
||||||
|
|
||||||
let isPumping = false;
|
|
||||||
let tempPressure = 0;
|
|
||||||
|
|
||||||
loop {
|
|
||||||
yield();
|
|
||||||
let temp = gasSensor.Temperature;
|
|
||||||
let pres = atmosAnal.Pressure;
|
|
||||||
let liqV = atmosAnal.VolumeOfLiquid;
|
|
||||||
let tempVol = atmosAnal.Volume;
|
|
||||||
|
|
||||||
let stress = 5_000 * liqV / tempVol;
|
|
||||||
|
|
||||||
tempPressure = isPumping ? 1_000 : 10_000;
|
|
||||||
|
|
||||||
let shouldTurnOnInlet = (
|
|
||||||
temp > 0c &&
|
|
||||||
pres < tempPressure &&
|
|
||||||
stress < 50
|
|
||||||
);
|
|
||||||
|
|
||||||
isPumping = (
|
|
||||||
!shouldTurnOnInlet &&
|
|
||||||
atmosTank.Pressure < 35_000 &&
|
|
||||||
atmosAnal.RatioPollutant == 0 &&
|
|
||||||
atmosAnal.RatioLiquidPollutant == 0 &&
|
|
||||||
atmosAnal.Pressure > 1_000
|
|
||||||
);
|
|
||||||
|
|
||||||
atmosValve.On = isPumping;
|
|
||||||
atmosInlet.On = shouldTurnOnInlet;
|
|
||||||
}
|
|
||||||
72
test.slang
72
test.slang
@@ -1,72 +0,0 @@
|
|||||||
/// Laree script V1
|
|
||||||
|
|
||||||
device self = "db";
|
|
||||||
device larre = "d0";
|
|
||||||
device exportChute = "d1";
|
|
||||||
|
|
||||||
const TOTAL_SLOTS = 19;
|
|
||||||
const EXPORT_CHUTE = 1;
|
|
||||||
const START_STATION = 2;
|
|
||||||
|
|
||||||
let currentIndex = 0;
|
|
||||||
|
|
||||||
/// Waits for the larre to be idle before continuing
|
|
||||||
fn waitForIdle() {
|
|
||||||
yield();
|
|
||||||
while (!larre.Idle) {
|
|
||||||
yield();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Instructs the Larre to go to the chute and deposit
|
|
||||||
/// what is currently in its arm
|
|
||||||
fn deposit() {
|
|
||||||
larre.Setting = EXPORT_CHUTE;
|
|
||||||
waitForIdle();
|
|
||||||
larre.Activate = true;
|
|
||||||
waitForIdle();
|
|
||||||
exportChute.Open = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This function is responsible for checking the plant under
|
|
||||||
/// the larre at this index, and harvesting if applicable
|
|
||||||
fn checkAndHarvest(currentIndex) {
|
|
||||||
if (currentIndex <= EXPORT_CHUTE || ls(larre, 255, "Seeding") < 1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// harvest from this device
|
|
||||||
while (ls(larre, 255, "Mature")) {
|
|
||||||
yield();
|
|
||||||
larre.Activate = true;
|
|
||||||
}
|
|
||||||
let hasRemainingPlant = ls(larre, 255, "Occupied");
|
|
||||||
|
|
||||||
// move to the export chute
|
|
||||||
larre.Setting = EXPORT_CHUTE;
|
|
||||||
waitForIdle();
|
|
||||||
deposit();
|
|
||||||
if (hasRemainingPlant) {
|
|
||||||
deposit();
|
|
||||||
}
|
|
||||||
|
|
||||||
larre.Setting = currentIndex;
|
|
||||||
waitForIdle();
|
|
||||||
|
|
||||||
if (ls(larre, 0, "Occupied")) {
|
|
||||||
larre.Activate = true;
|
|
||||||
}
|
|
||||||
waitForIdle();
|
|
||||||
}
|
|
||||||
|
|
||||||
loop {
|
|
||||||
yield();
|
|
||||||
if (!larre.Idle) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let newIndex = currentIndex + 1 > TOTAL_SLOTS ? START_STATION : currentIndex + 1;
|
|
||||||
|
|
||||||
checkAndHarvest(currentIndex);
|
|
||||||
larre.Setting = newIndex;
|
|
||||||
currentIndex = newIndex;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user