🤔 Add a type inference engine, along with typed literals. #4

Merged
acw merged 25 commits from acw/type-checker into develop 2023-09-19 20:40:05 -07:00
Showing only changes of commit 12057a35b2 - Show all commits

View File

@@ -45,22 +45,11 @@ pub enum PrimOpError {
macro_rules! run_op {
($op: ident, $left: expr, $right: expr) => {
match $op {
"+" => $left
.checked_add($right)
.ok_or(PrimOpError::MathFailure("+"))
.map(Into::into),
"-" => $left
.checked_sub($right)
.ok_or(PrimOpError::MathFailure("-"))
.map(Into::into),
"*" => $left
.checked_mul($right)
.ok_or(PrimOpError::MathFailure("*"))
.map(Into::into),
"/" => $left
.checked_div($right)
.ok_or(PrimOpError::MathFailure("/"))
.map(Into::into),
"+" => Ok($left.wrapping_add($right).into()),
"-" => Ok($left.wrapping_sub($right).into()),
"*" => Ok($left.wrapping_mul($right).into()),
"/" if $right == 0 => Err(PrimOpError::MathFailure("/")),
"/" => Ok($left.wrapping_div($right).into()),
_ => Err(PrimOpError::UnknownPrimOp($op.to_string())),
}
};
@@ -70,10 +59,10 @@ impl Value {
fn unary_op(operation: &str, value: &Value) -> Result<Value, PrimOpError> {
match operation {
"-" => match value {
Value::I8(x) => Ok(Value::I8(-x)),
Value::I16(x) => Ok(Value::I16(-x)),
Value::I32(x) => Ok(Value::I32(-x)),
Value::I64(x) => Ok(Value::I64(-x)),
Value::I8(x) => Ok(Value::I8(x.wrapping_neg())),
Value::I16(x) => Ok(Value::I16(x.wrapping_neg())),
Value::I32(x) => Ok(Value::I32(x.wrapping_neg())),
Value::I64(x) => Ok(Value::I64(x.wrapping_neg())),
_ => Err(PrimOpError::BadTypeFor("-", value.clone())),
},
_ => Err(PrimOpError::BadArgCount(operation.to_owned(), 1)),