🤔 Add a type inference engine, along with typed literals. (#4)
The typed literal formatting mirrors that of Rust. If no type can be inferred for an untagged literal, the type inference engine will warn the user and then assume that they meant an unsigned 64-bit number. (This is slightly inconvenient, because there can be cases in which our Arbitrary instance may generate a unary negation, in which we should assume that it's a signed 64-bit number; we may want to revisit this later.) The type inference engine is a standard two phase one, in which we first generate a series of type constraints, and then we solve those constraints. In this particular implementation, we actually use a third phase to generate a final AST. Finally, to increase the amount of testing performed, I've removed the overflow checking in the evaluator. The only thing we now check for is division by zero. This does make things a trace slower in testing, but hopefully we get more coverage this way.
This commit was merged in pull request #4.
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
use internment::ArcIntern;
|
||||
|
||||
use crate::eval::{EvalEnvironment, EvalError, Value};
|
||||
use crate::syntax::{Expression, Program, Statement};
|
||||
use crate::eval::{EvalEnvironment, EvalError, PrimitiveType, Value};
|
||||
use crate::syntax::{ConstantType, Expression, Program, Statement};
|
||||
use std::str::FromStr;
|
||||
|
||||
impl Program {
|
||||
/// Evaluate the program, returning either an error or what it prints out when run.
|
||||
@@ -24,11 +25,11 @@ impl Program {
|
||||
match stmt {
|
||||
Statement::Binding(_, name, value) => {
|
||||
let actual_value = value.eval(&env)?;
|
||||
env = env.extend(ArcIntern::new(name.clone()), actual_value);
|
||||
env = env.extend(name.clone().intern(), actual_value);
|
||||
}
|
||||
|
||||
Statement::Print(_, name) => {
|
||||
let value = env.lookup(ArcIntern::new(name.clone()))?;
|
||||
let value = env.lookup(name.clone().intern())?;
|
||||
let line = format!("{} = {}\n", name, value);
|
||||
stdout.push_str(&line);
|
||||
}
|
||||
@@ -43,11 +44,28 @@ impl Expression {
|
||||
fn eval(&self, env: &EvalEnvironment) -> Result<Value, EvalError> {
|
||||
match self {
|
||||
Expression::Value(_, v) => match v {
|
||||
super::Value::Number(_, v) => Ok(Value::I64(*v)),
|
||||
super::Value::Number(_, ty, v) => match ty {
|
||||
None => Ok(Value::U64(*v)),
|
||||
// FIXME: make these types validate their input size
|
||||
Some(ConstantType::I8) => Ok(Value::I8(*v as i8)),
|
||||
Some(ConstantType::I16) => Ok(Value::I16(*v as i16)),
|
||||
Some(ConstantType::I32) => Ok(Value::I32(*v as i32)),
|
||||
Some(ConstantType::I64) => Ok(Value::I64(*v as i64)),
|
||||
Some(ConstantType::U8) => Ok(Value::U8(*v as u8)),
|
||||
Some(ConstantType::U16) => Ok(Value::U16(*v as u16)),
|
||||
Some(ConstantType::U32) => Ok(Value::U32(*v as u32)),
|
||||
Some(ConstantType::U64) => Ok(Value::U64(*v)),
|
||||
},
|
||||
},
|
||||
|
||||
Expression::Reference(_, n) => Ok(env.lookup(ArcIntern::new(n.clone()))?),
|
||||
|
||||
Expression::Cast(_, target, expr) => {
|
||||
let target_type = PrimitiveType::from_str(target)?;
|
||||
let value = expr.eval(env)?;
|
||||
Ok(target_type.safe_cast(&value)?)
|
||||
}
|
||||
|
||||
Expression::Primitive(_, op, args) => {
|
||||
let mut arg_values = Vec::with_capacity(args.len());
|
||||
|
||||
@@ -66,12 +84,12 @@ impl Expression {
|
||||
fn two_plus_three() {
|
||||
let input = Program::parse(0, "x = 2 + 3; print x;").expect("parse works");
|
||||
let output = input.eval().expect("runs successfully");
|
||||
assert_eq!("x = 5i64\n", &output);
|
||||
assert_eq!("x = 5u64\n", &output);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lotsa_math() {
|
||||
let input = Program::parse(0, "x = 2 + 3 * 10 / 5 - 1; print x;").expect("parse works");
|
||||
let output = input.eval().expect("runs successfully");
|
||||
assert_eq!("x = 7i64\n", &output);
|
||||
assert_eq!("x = 7u64\n", &output);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user