Compare commits
3 Commits
bad4713779
...
e15f77d9de
Author | SHA1 | Date | |
---|---|---|---|
e15f77d9de | |||
bc1cc99bcc | |||
b64cdc4c71 |
10
README.md
10
README.md
@ -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 fibonacci` or
|
Currently when testing the compiler I run `./libtest.sh reid_src/{file}.reid`,
|
||||||
`./libtest.sh arithmetic`.
|
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
|
What `./libtest.sh $1` does, is it compiles and runs the rust example found at
|
||||||
name `$1`, which may exist in any of the two projects. The two mentioned above
|
path `$1`. Some pre-existing examples can be found in [`reid_src`](./reid_src)
|
||||||
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
|
||||||
|
@ -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 $1 && \
|
cargo run --example cli $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 && \
|
||||||
|
@ -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
18
reid/examples/cli.rs
Normal 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(())
|
||||||
|
}
|
@ -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);
|
|
||||||
}
|
|
@ -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);
|
|
||||||
}
|
|
@ -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));
|
|
@ -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:
|
||||||
//! ```rust
|
//! ```reid
|
||||||
//! fn main() -> u16 {
|
//! fn main() -> u16 {
|
||||||
//! return fibonacci(5);
|
//! return fibonacci(5);
|
||||||
//! }
|
//! }
|
||||||
@ -30,6 +30,7 @@
|
|||||||
//! }
|
//! }
|
||||||
//! 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)
|
||||||
|
@ -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(Vague(Unknown)),
|
FunctionDefinitionKind::Extern => Ok((ReturnKind::Soft, Vague(Unknown))),
|
||||||
};
|
};
|
||||||
|
|
||||||
match inferred {
|
match inferred {
|
||||||
Ok(t) => return_type
|
Ok(t) => return_type
|
||||||
.collapse_into(&t)
|
.collapse_into(&t.1)
|
||||||
.or(Err(ErrorKind::ReturnTypeMismatch(return_type, t))),
|
.or(Err(ErrorKind::ReturnTypeMismatch(return_type, t.1))),
|
||||||
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<TypeKind, ErrorKind> {
|
) -> Result<(ReturnKind, 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(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 {
|
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(state.or_else(res, Vague(Unknown), expr.1))
|
Ok((*return_kind, state.or_else(res, Vague(Unknown), expr.1)))
|
||||||
} else {
|
} else {
|
||||||
Ok(Void)
|
Ok((ReturnKind::Soft, Void))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -359,14 +359,27 @@ 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_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 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);
|
||||||
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 {
|
||||||
// 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
|
||||||
@ -384,7 +397,11 @@ impl Expression {
|
|||||||
|
|
||||||
Ok(collapsed)
|
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) => {
|
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 {
|
||||||
|
@ -5,8 +5,6 @@ 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;
|
@ -6,11 +6,11 @@ fn indirection() -> bool {
|
|||||||
|
|
||||||
fn main() -> u16 {
|
fn main() -> u16 {
|
||||||
let mut test = 5;
|
let mut test = 5;
|
||||||
let heehoo = 10;
|
let addition = 10;
|
||||||
|
|
||||||
if indirection() {
|
if indirection() {
|
||||||
test = 11;
|
test = 11;
|
||||||
}
|
}
|
||||||
|
|
||||||
return test + heehoo;
|
return test + addition;
|
||||||
}
|
}
|
7
reid_src/strings.reid
Normal file
7
reid_src/strings.reid
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
|
||||||
|
|
||||||
|
fn main() -> u16 {
|
||||||
|
let hello = "beep boop";
|
||||||
|
|
||||||
|
return 5;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user