Compare commits

..

No commits in common. "e15f77d9dec6f8e165c23dee38e73c03a460c0e5" and "bad4713779dd98bbd97d75ebd5e4f5a8050148f8" have entirely different histories.

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? ## What is currently being tested?
Currently when testing the compiler I run `./libtest.sh reid_src/{file}.reid`, Currently when testing the compiler I run `./libtest.sh fibonacci` or
where the `{file}` is one of the various examples I've written to help me test `./libtest.sh arithmetic`.
features of the compiler.
What `./libtest.sh $1` does, is it compiles and runs the rust example found at What `./libtest.sh $1` does, is it compiles and runs the rust example found with
path `$1`. Some pre-existing examples can be found in [`reid_src`](./reid_src) name `$1`, which may exist in any of the two projects. The two mentioned above
are in `reid/examples`.
All examples currently end up producing a `hello.o` and `hello.asm` file to the 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 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 # Do note this file is extremely simply for my own personal convenience
export .env export .env
cargo run --example cli $1 && \ cargo run --example $1 && \
# clang hello.o -o main && \ # clang hello.o -o main && \
ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 \ ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 \
-o main /usr/lib/crt1.o hello.o -lc && \ -o main /usr/lib/crt1.o hello.o -lc && \

11
reid/examples/array.rs Normal file
View File

@ -0,0 +1,11 @@
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);
}

View File

@ -1,18 +0,0 @@
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

@ -0,0 +1,11 @@
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);
}

11
reid/examples/mutable.rs Normal file
View File

@ -0,0 +1,11 @@
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

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

View File

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

View File

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

View File

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

View File

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

View File

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