mirror of
https://github.com/HigherOrderCO/Kind1.git
synced 2024-09-11 08:45:32 +03:00
Fix: fix kdl compilation, some formatting
This commit is contained in:
parent
7de1ff1808
commit
767225031d
@ -23,12 +23,7 @@ impl<'a> DesugarState<'a> {
|
||||
literal: &expr::Literal,
|
||||
) -> Box<desugared::Expr> {
|
||||
match literal {
|
||||
Literal::Number(kind_tree::Number::U120(num)) => {
|
||||
if !self.check_implementation("U120.new", range, Sugar::U120) {
|
||||
return desugared::Expr::err(range);
|
||||
}
|
||||
desugared::Expr::num120(range, *num)
|
||||
}
|
||||
Literal::Number(kind_tree::Number::U120(num)) => desugared::Expr::num120(range, *num),
|
||||
Literal::String(string) => {
|
||||
if !self.check_implementation("String.cons", range, Sugar::String)
|
||||
|| !self.check_implementation("String.nil", range, Sugar::String)
|
||||
|
@ -9,7 +9,6 @@ pub enum Sugar {
|
||||
Pair,
|
||||
BoolIf,
|
||||
String,
|
||||
U120,
|
||||
Match(String),
|
||||
Open(String),
|
||||
}
|
||||
@ -220,7 +219,6 @@ impl Diagnostic for PassError {
|
||||
Sugar::Pair => "You must implement 'Sigma' and 'Sigma.new' in order to use the sigma notation.".to_string(),
|
||||
Sugar::BoolIf => "You must implement 'Bool.if' in order to use the if notation.".to_string(),
|
||||
Sugar::String => "You must implement 'String.cons' in order to use the string notation.".to_string(),
|
||||
Sugar::U120 => "You must implement 'U120.new' in order to use the u120 notation.".to_string(),
|
||||
Sugar::Match(name) => format!("You must implement '{}.match' in order to use the match notation (or derive match with #derive[match]).", name),
|
||||
Sugar::Open(name) => format!("You must implement '{}.open' in order to use the open notation (or derive open with #derive[open]).", name),
|
||||
}],
|
||||
|
@ -133,7 +133,7 @@ pub fn compile_rule(ctx: &mut CompileCtx, rule: &untyped::Rule) -> kindelia_lang
|
||||
let arg = compile_expr(ctx, pat);
|
||||
args.push(arg);
|
||||
}
|
||||
let lhs = kdl::Term::fun(name, args);
|
||||
let lhs = kdl::Term::ctr(name, args);
|
||||
let rhs = compile_expr(ctx, &rule.body);
|
||||
let rule = kdl::Rule { lhs, rhs };
|
||||
rule
|
||||
@ -202,13 +202,10 @@ pub fn compile_expr(ctx: &mut CompileCtx, expr: &untyped::Expr) -> kindelia_lang
|
||||
}
|
||||
}
|
||||
From::Ctr { name, args } => {
|
||||
let name = ctx.kdl_names.get(name.to_str()).unwrap().clone();
|
||||
let args = args.iter().map(|x| compile_expr(ctx, &x)).collect();
|
||||
To::Ctr { name, args }
|
||||
}
|
||||
From::Fun { name, args } => {
|
||||
match name.to_str() {
|
||||
// Special compilation for some numeric functions
|
||||
// They have no rules because they're compilation defined,
|
||||
// so they've been initially interpreted as Ctr
|
||||
|
||||
// Add with no boundary check is just a normal add
|
||||
"U60.add_unsafe" => To::Op2 {
|
||||
@ -306,19 +303,19 @@ pub fn compile_expr(ctx: &mut CompileCtx, expr: &untyped::Expr) -> kindelia_lang
|
||||
val0: Box::new(compile_expr(ctx, &args[0])),
|
||||
val1: Box::new(compile_expr(ctx, &args[1])),
|
||||
},
|
||||
// All other functions with normal compilation
|
||||
|
||||
// All other constructors have a normal compilation
|
||||
_ => {
|
||||
let name = ctx.kdl_names.get(name.to_str()).unwrap().clone();
|
||||
let mut new_args = Vec::new();
|
||||
for arg in args {
|
||||
new_args.push(compile_expr(ctx, &arg));
|
||||
}
|
||||
To::Fun {
|
||||
name,
|
||||
args: new_args,
|
||||
let args = args.iter().map(|x| compile_expr(ctx, &x)).collect();
|
||||
To::Ctr { name, args }
|
||||
}
|
||||
}
|
||||
}
|
||||
From::Fun { name, args } => {
|
||||
let name = ctx.kdl_names.get(name.to_str()).unwrap().clone();
|
||||
let args = args.iter().map(|x| compile_expr(ctx, x)).collect();
|
||||
To::Fun { name, args }
|
||||
}
|
||||
From::Lambda {
|
||||
param,
|
||||
@ -418,13 +415,15 @@ pub fn compile_entry(ctx: &mut CompileCtx, entry: &untyped::Entry) {
|
||||
}
|
||||
|
||||
if entry.rules.len() == 0 {
|
||||
// Functions with no rules become Ctr
|
||||
let sttm = kdl::Statement::Ctr {
|
||||
name,
|
||||
args,
|
||||
sign: None,
|
||||
};
|
||||
ctx.file.funs.insert(entry.name.to_string(), sttm);
|
||||
ctx.file.ctrs.insert(entry.name.to_string(), sttm);
|
||||
} else {
|
||||
// Functions with rules become Fun
|
||||
let rules = entry
|
||||
.rules
|
||||
.iter()
|
||||
|
@ -9,6 +9,8 @@
|
||||
// - results in: `(Foo x0 *) = dup x0.0 x0.1 = x0; (+ x0.0 x0.1)`
|
||||
// The algorithm was copied from the hvm
|
||||
|
||||
// TODO: This is inserting unneeded `let`s for all linear rule variables
|
||||
|
||||
use crate::File;
|
||||
use fxhash::FxHashMap;
|
||||
use kindelia_lang::ast::{Func, Name, Rule, Statement, Term};
|
||||
@ -57,11 +59,17 @@ impl LinearizeCtx {
|
||||
}
|
||||
}
|
||||
Term::Num { .. } => (),
|
||||
_ => unreachable!(), // Invalid lhs param
|
||||
_ => unreachable!(
|
||||
"Invalid left-hand side parameter. Expected Var, Ctr or Num, got {:?}",
|
||||
arg
|
||||
),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
unreachable!(); // Invalid lhs Term
|
||||
unreachable!(
|
||||
"Invalid left-hand side term. Expected Ctr, got {:?}",
|
||||
rule.lhs
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -106,7 +114,7 @@ pub fn linearize_file(file: File) -> File {
|
||||
};
|
||||
funs.insert(kind_name, stmt);
|
||||
} else {
|
||||
unreachable!();
|
||||
unreachable!("Expected list of Funs, found {:?}", stmt);
|
||||
}
|
||||
}
|
||||
let ctrs = file.ctrs;
|
||||
@ -148,7 +156,7 @@ pub fn linearize_term(ctx: &mut LinearizeCtx, term: &Term, lhs: bool) -> Box<Ter
|
||||
let name = Name::from_str(&format!("{}.{}", name, used - 1)).unwrap(); // TODO: Think if this errs or not
|
||||
Term::Var { name }
|
||||
} else {
|
||||
unreachable!(); // Is it? Unbound variable
|
||||
unreachable!("Unbound variable '{}' in kdl compilation", name.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user