🤔 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:
@@ -8,6 +8,8 @@ use std::fmt::Write;
|
||||
use target_lexicon::Triple;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::syntax::ConstantType;
|
||||
|
||||
/// An object for querying / using functions built into the runtime.
|
||||
///
|
||||
/// Right now, this is a quite a bit of boilerplate for very nebulous
|
||||
@@ -49,7 +51,7 @@ impl RuntimeFunctions {
|
||||
"print",
|
||||
Linkage::Import,
|
||||
&Signature {
|
||||
params: vec![string_param, string_param, int64_param],
|
||||
params: vec![string_param, string_param, int64_param, int64_param],
|
||||
returns: vec![],
|
||||
call_conv: CallConv::triple_default(platform),
|
||||
},
|
||||
@@ -98,13 +100,30 @@ impl RuntimeFunctions {
|
||||
// we extend with the output, so that multiple JIT'd `Program`s can run concurrently
|
||||
// without stomping over each other's output. If `output_buffer` is NULL, we just print
|
||||
// to stdout.
|
||||
extern "C" fn runtime_print(output_buffer: *mut String, name: *const i8, value: i64) {
|
||||
extern "C" fn runtime_print(
|
||||
output_buffer: *mut String,
|
||||
name: *const i8,
|
||||
vtype_repr: i64,
|
||||
value: i64,
|
||||
) {
|
||||
let cstr = unsafe { CStr::from_ptr(name) };
|
||||
let reconstituted = cstr.to_string_lossy();
|
||||
|
||||
let output = match vtype_repr.try_into() {
|
||||
Ok(ConstantType::I8) => format!("{} = {}i8", reconstituted, value as i8),
|
||||
Ok(ConstantType::I16) => format!("{} = {}i16", reconstituted, value as i16),
|
||||
Ok(ConstantType::I32) => format!("{} = {}i32", reconstituted, value as i32),
|
||||
Ok(ConstantType::I64) => format!("{} = {}i64", reconstituted, value),
|
||||
Ok(ConstantType::U8) => format!("{} = {}u8", reconstituted, value as u8),
|
||||
Ok(ConstantType::U16) => format!("{} = {}u16", reconstituted, value as u16),
|
||||
Ok(ConstantType::U32) => format!("{} = {}u32", reconstituted, value as u32),
|
||||
Ok(ConstantType::U64) => format!("{} = {}u64", reconstituted, value as u64),
|
||||
Err(_) => format!("{} = {}<unknown type>", reconstituted, value),
|
||||
};
|
||||
|
||||
if let Some(output_buffer) = unsafe { output_buffer.as_mut() } {
|
||||
writeln!(output_buffer, "{} = {}i64", reconstituted, value).unwrap();
|
||||
writeln!(output_buffer, "{}", output).unwrap();
|
||||
} else {
|
||||
println!("{} = {}", reconstituted, value);
|
||||
println!("{}", output);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user