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