Support for bools. Also fix fallible/infallible mixup for ctors.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
(type u32 (primitive u32))
|
||||
(type bool (primitive bool))
|
||||
(type A (enum (A1 (x u32))))
|
||||
|
||||
(decl Ext1 (u32) A)
|
||||
@@ -6,6 +7,9 @@
|
||||
(extern extractor Ext1 ext1)
|
||||
(extern extractor Ext2 ext2)
|
||||
|
||||
(decl C (bool) A)
|
||||
(extern constructor C c)
|
||||
|
||||
(decl Lower (A) A)
|
||||
|
||||
(rule
|
||||
@@ -14,4 +18,4 @@
|
||||
a
|
||||
(Ext1 x)
|
||||
(Ext2 =x)))
|
||||
a)
|
||||
(C #t))
|
||||
|
||||
@@ -711,6 +711,18 @@ impl<'a> Codegen<'a> {
|
||||
ctx.values.insert(value.clone(), (is_ref, ty));
|
||||
}
|
||||
|
||||
fn const_int(&self, val: i64, ty: TypeId) -> String {
|
||||
let is_bool = match &self.typeenv.types[ty.index()] {
|
||||
&Type::Primitive(_, name) => &self.typeenv.syms[name.index()] == "bool",
|
||||
_ => unreachable!(),
|
||||
};
|
||||
if is_bool {
|
||||
format!("{}", val != 0)
|
||||
} else {
|
||||
format!("{}", val)
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_internal_term_constructors(&self, code: &mut dyn Write) -> Result<(), Error> {
|
||||
for (&termid, trie) in &self.functions_by_term {
|
||||
let termdata = &self.termenv.terms[termid.index()];
|
||||
@@ -775,8 +787,15 @@ impl<'a> Codegen<'a> {
|
||||
};
|
||||
self.define_val(&value, ctx, /* is_ref = */ false, ty);
|
||||
let name = self.value_name(&value);
|
||||
let ty = self.type_name(ty, /* by_ref = */ false);
|
||||
writeln!(code, "{}let {}: {} = {};", indent, name, ty, val)?;
|
||||
let ty_name = self.type_name(ty, /* by_ref = */ false);
|
||||
writeln!(
|
||||
code,
|
||||
"{}let {}: {} = {};",
|
||||
indent,
|
||||
name,
|
||||
ty_name,
|
||||
self.const_int(val, ty)
|
||||
)?;
|
||||
}
|
||||
&ExprInst::CreateVariant {
|
||||
ref inputs,
|
||||
@@ -1027,7 +1046,7 @@ impl<'a> Codegen<'a> {
|
||||
"{}let {} = {};",
|
||||
indent,
|
||||
self.value_name(&output),
|
||||
val
|
||||
self.const_int(val, ty),
|
||||
)?;
|
||||
self.define_val(&output, ctx, /* is_ref = */ false, ty);
|
||||
Ok(true)
|
||||
|
||||
@@ -519,7 +519,7 @@ impl ExprSequence {
|
||||
&arg_values_tys[..],
|
||||
ty,
|
||||
term,
|
||||
/* infallible = */ true,
|
||||
/* infallible = */ false,
|
||||
)
|
||||
}
|
||||
&TermKind::ExternalConstructor { .. } => {
|
||||
@@ -527,7 +527,7 @@ impl ExprSequence {
|
||||
&arg_values_tys[..],
|
||||
ty,
|
||||
term,
|
||||
/* infallible = */ false,
|
||||
/* infallible = */ true,
|
||||
)
|
||||
}
|
||||
_ => panic!("Should have been caught by typechecking"),
|
||||
|
||||
@@ -423,6 +423,12 @@ impl<'a> Parser<'a> {
|
||||
self.rparen()?;
|
||||
Ok(Expr::Term { sym, args })
|
||||
}
|
||||
} else if self.is_sym_str("#t") {
|
||||
self.symbol()?;
|
||||
Ok(Expr::ConstInt { val: 1 })
|
||||
} else if self.is_sym_str("#f") {
|
||||
self.symbol()?;
|
||||
Ok(Expr::ConstInt { val: 0 })
|
||||
} else if self.is_sym() {
|
||||
let name = self.parse_ident()?;
|
||||
Ok(Expr::Var { name })
|
||||
|
||||
Reference in New Issue
Block a user