Compare commits

...

3 Commits

Author SHA1 Message Date
e15f77d9de Update libtest and make examples/cli.rs 2025-07-14 17:26:38 +03:00
bc1cc99bcc Update examples 2025-07-14 17:19:10 +03:00
b64cdc4c71 Fix if-statement block return type evaluation 2025-07-14 17:17:48 +03:00
13 changed files with 62 additions and 62 deletions

View File

@ -57,12 +57,12 @@ and not as representative of my skills as a programmer today as this one.
## What is currently being tested?
Currently when testing the compiler I run `./libtest.sh fibonacci` or
`./libtest.sh arithmetic`.
Currently when testing the compiler I run `./libtest.sh reid_src/{file}.reid`,
where the `{file}` is one of the various examples I've written to help me test
features of the compiler.
What `./libtest.sh $1` does, is it compiles and runs the rust example found with
name `$1`, which may exist in any of the two projects. The two mentioned above
are in `reid/examples`.
What `./libtest.sh $1` does, is it compiles and runs the rust example found at
path `$1`. Some pre-existing examples can be found in [`reid_src`](./reid_src)
All examples currently end up producing a `hello.o` and `hello.asm` file to the
root directory, which is then linked with `ldd` to produce a `main`, which is

View File

@ -6,7 +6,7 @@
# Do note this file is extremely simply for my own personal convenience
export .env
cargo run --example $1 && \
cargo run --example cli $1 && \
# clang hello.o -o main && \
ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 \
-o main /usr/lib/crt1.o hello.o -lc && \

View File

@ -1,11 +0,0 @@
use reid::compile;
pub static ARRAY: &str = include_str!("./reid/array.reid");
fn main() {
let text = match compile(ARRAY) {
Ok(t) => t,
Err(e) => panic!("{}", e),
};
println!("{}", text);
}

18
reid/examples/cli.rs Normal file
View File

@ -0,0 +1,18 @@
use std::{env, fs};
use reid::compile;
fn main() -> Result<(), std::io::Error> {
let args: Vec<String> = env::args().collect();
if let Some(filename) = args.get(1) {
let text = fs::read_to_string(filename)?;
let output = match compile(&text) {
Ok(t) => t,
Err(e) => panic!("{}", e),
};
println!("{}", output);
} else {
println!("Please input compiled file path!")
}
Ok(())
}

View File

@ -1,11 +0,0 @@
use reid::compile;
pub static FIBONACCI: &str = include_str!("./reid/fibonacci.reid");
fn main() {
let text = match compile(FIBONACCI) {
Ok(t) => t,
Err(e) => panic!("{}", e),
};
println!("{}", text);
}

View File

@ -1,11 +0,0 @@
use reid::compile;
pub static MUTABLE: &str = include_str!("./reid/mutable.reid");
fn main() {
let text = match compile(MUTABLE) {
Ok(t) => t,
Err(e) => panic!("{}", e),
};
println!("{}", text);
}

View File

@ -1,8 +0,0 @@
// New types, type-casting
import std::print;
let text: string = "hello there!";
let value: i16 = 123;
print(text + (value as string));

View File

@ -19,7 +19,7 @@
//!
//! An example program of Reid, that calculates the 5th fibonacci number (and
//! uses Rust for highlighting) is:
//! ```rust
//! ```reid
//! fn main() -> u16 {
//! return fibonacci(5);
//! }
@ -30,6 +30,7 @@
//! }
//! return fibonacci(n-1) + fibonacci(n-2);
//! }
//! ```
//!
//! Currently missing relevant features (TODOs) are:
//! - ~~Arrays~~ (DONE)

View File

@ -91,13 +91,13 @@ impl FunctionDefinition {
state.scope.return_type_hint = Some(self.return_type.clone());
block.typecheck(state, &hints, Some(&return_type))
}
FunctionDefinitionKind::Extern => Ok(Vague(Unknown)),
FunctionDefinitionKind::Extern => Ok((ReturnKind::Soft, Vague(Unknown))),
};
match inferred {
Ok(t) => return_type
.collapse_into(&t)
.or(Err(ErrorKind::ReturnTypeMismatch(return_type, t))),
.collapse_into(&t.1)
.or(Err(ErrorKind::ReturnTypeMismatch(return_type, t.1))),
Err(e) => Ok(state.or_else(Err(e), return_type, self.block_meta())),
}
}
@ -109,7 +109,7 @@ impl Block {
state: &mut PassState<ErrorKind>,
hints: &TypeRefs,
hint_t: Option<&TypeKind>,
) -> Result<TypeKind, ErrorKind> {
) -> Result<(ReturnKind, TypeKind), ErrorKind> {
let mut state = state.inner();
let mut early_return = None;
@ -233,7 +233,7 @@ impl Block {
if let Some((ReturnKind::Hard, expr)) = early_return {
let hint = state.scope.return_type_hint.clone();
let res = expr.typecheck(&mut state, &hints, hint.as_ref());
return Ok(state.or_else(res, Vague(Unknown), expr.1));
return Ok((ReturnKind::Hard, state.or_else(res, Vague(Unknown), expr.1)));
}
if let Some((return_kind, expr)) = &mut self.return_expression {
@ -243,9 +243,9 @@ impl Block {
ReturnKind::Soft => hint_t.cloned(),
};
let res = expr.typecheck(&mut state, &hints, ret_hint_t.as_ref());
Ok(state.or_else(res, Vague(Unknown), expr.1))
Ok((*return_kind, state.or_else(res, Vague(Unknown), expr.1)))
} else {
Ok(Void)
Ok((ReturnKind::Soft, Void))
}
}
}
@ -359,14 +359,27 @@ impl Expression {
// Typecheck then/else return types and make sure they are the
// same, if else exists.
let then_res = lhs.typecheck(state, &hints, hint_t);
let then_ret_t = state.or_else(then_res, Vague(Unknown), lhs.meta);
let (then_ret_kind, then_ret_t) =
state.or_else(then_res, (ReturnKind::Soft, Vague(Unknown)), lhs.meta);
let else_ret_t = if let Some(else_block) = rhs {
let res = else_block.typecheck(state, &hints, hint_t);
state.or_else(res, Vague(Unknown), else_block.meta)
let (else_ret_kind, else_ret_t) =
state.or_else(res, (ReturnKind::Soft, Vague(Unknown)), else_block.meta);
if else_ret_kind == ReturnKind::Hard {
Void
} else {
else_ret_t
}
} else {
// Else return type is Void if it does not exist
Void
};
let then_ret_t = if then_ret_kind == ReturnKind::Hard {
Void
} else {
then_ret_t
};
// Make sure then and else -blocks have the same return type
let collapsed = then_ret_t
@ -384,7 +397,11 @@ impl Expression {
Ok(collapsed)
}
ExprKind::Block(block) => block.typecheck(state, &hints, hint_t),
ExprKind::Block(block) => match block.typecheck(state, &hints, hint_t) {
Ok((ReturnKind::Hard, _)) => Ok(Void),
Ok((_, ty)) => Ok(ty),
Err(e) => Err(e),
},
ExprKind::Index(expression, elem_ty, idx) => {
// Try to unwrap hint type from array if possible
let hint_t = hint_t.map(|t| match t {

View File

@ -5,8 +5,6 @@ fn array() -> [[[u16; 4]; 1]; 1] {
}
fn main() -> u16 {
let heehoo = 10;
let mut list = array();
list[0][0][3] = 5;

View File

@ -6,11 +6,11 @@ fn indirection() -> bool {
fn main() -> u16 {
let mut test = 5;
let heehoo = 10;
let addition = 10;
if indirection() {
test = 11;
}
return test + heehoo;
return test + addition;
}

7
reid_src/strings.reid Normal file
View File

@ -0,0 +1,7 @@
fn main() -> u16 {
let hello = "beep boop";
return 5;
}