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,
|
literal: &expr::Literal,
|
||||||
) -> Box<desugared::Expr> {
|
) -> Box<desugared::Expr> {
|
||||||
match literal {
|
match literal {
|
||||||
Literal::Number(kind_tree::Number::U120(num)) => {
|
Literal::Number(kind_tree::Number::U120(num)) => desugared::Expr::num120(range, *num),
|
||||||
if !self.check_implementation("U120.new", range, Sugar::U120) {
|
|
||||||
return desugared::Expr::err(range);
|
|
||||||
}
|
|
||||||
desugared::Expr::num120(range, *num)
|
|
||||||
}
|
|
||||||
Literal::String(string) => {
|
Literal::String(string) => {
|
||||||
if !self.check_implementation("String.cons", range, Sugar::String)
|
if !self.check_implementation("String.cons", range, Sugar::String)
|
||||||
|| !self.check_implementation("String.nil", range, Sugar::String)
|
|| !self.check_implementation("String.nil", range, Sugar::String)
|
||||||
|
@ -9,7 +9,6 @@ pub enum Sugar {
|
|||||||
Pair,
|
Pair,
|
||||||
BoolIf,
|
BoolIf,
|
||||||
String,
|
String,
|
||||||
U120,
|
|
||||||
Match(String),
|
Match(String),
|
||||||
Open(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::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::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::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::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),
|
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);
|
let arg = compile_expr(ctx, pat);
|
||||||
args.push(arg);
|
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 rhs = compile_expr(ctx, &rule.body);
|
||||||
let rule = kdl::Rule { lhs, rhs };
|
let rule = kdl::Rule { lhs, rhs };
|
||||||
rule
|
rule
|
||||||
@ -202,13 +202,10 @@ pub fn compile_expr(ctx: &mut CompileCtx, expr: &untyped::Expr) -> kindelia_lang
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
From::Ctr { name, args } => {
|
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() {
|
match name.to_str() {
|
||||||
// Special compilation for some numeric functions
|
// 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
|
// Add with no boundary check is just a normal add
|
||||||
"U60.add_unsafe" => To::Op2 {
|
"U60.add_unsafe" => To::Op2 {
|
||||||
@ -306,20 +303,20 @@ pub fn compile_expr(ctx: &mut CompileCtx, expr: &untyped::Expr) -> kindelia_lang
|
|||||||
val0: Box::new(compile_expr(ctx, &args[0])),
|
val0: Box::new(compile_expr(ctx, &args[0])),
|
||||||
val1: Box::new(compile_expr(ctx, &args[1])),
|
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 name = ctx.kdl_names.get(name.to_str()).unwrap().clone();
|
||||||
let mut new_args = Vec::new();
|
let args = args.iter().map(|x| compile_expr(ctx, &x)).collect();
|
||||||
for arg in args {
|
To::Ctr { name, args }
|
||||||
new_args.push(compile_expr(ctx, &arg));
|
|
||||||
}
|
|
||||||
To::Fun {
|
|
||||||
name,
|
|
||||||
args: new_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 {
|
From::Lambda {
|
||||||
param,
|
param,
|
||||||
body,
|
body,
|
||||||
@ -418,13 +415,15 @@ pub fn compile_entry(ctx: &mut CompileCtx, entry: &untyped::Entry) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if entry.rules.len() == 0 {
|
if entry.rules.len() == 0 {
|
||||||
|
// Functions with no rules become Ctr
|
||||||
let sttm = kdl::Statement::Ctr {
|
let sttm = kdl::Statement::Ctr {
|
||||||
name,
|
name,
|
||||||
args,
|
args,
|
||||||
sign: None,
|
sign: None,
|
||||||
};
|
};
|
||||||
ctx.file.funs.insert(entry.name.to_string(), sttm);
|
ctx.file.ctrs.insert(entry.name.to_string(), sttm);
|
||||||
} else {
|
} else {
|
||||||
|
// Functions with rules become Fun
|
||||||
let rules = entry
|
let rules = entry
|
||||||
.rules
|
.rules
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
// - results in: `(Foo x0 *) = dup x0.0 x0.1 = x0; (+ x0.0 x0.1)`
|
// - results in: `(Foo x0 *) = dup x0.0 x0.1 = x0; (+ x0.0 x0.1)`
|
||||||
// The algorithm was copied from the hvm
|
// The algorithm was copied from the hvm
|
||||||
|
|
||||||
|
// TODO: This is inserting unneeded `let`s for all linear rule variables
|
||||||
|
|
||||||
use crate::File;
|
use crate::File;
|
||||||
use fxhash::FxHashMap;
|
use fxhash::FxHashMap;
|
||||||
use kindelia_lang::ast::{Func, Name, Rule, Statement, Term};
|
use kindelia_lang::ast::{Func, Name, Rule, Statement, Term};
|
||||||
@ -57,11 +59,17 @@ impl LinearizeCtx {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Term::Num { .. } => (),
|
Term::Num { .. } => (),
|
||||||
_ => unreachable!(), // Invalid lhs param
|
_ => unreachable!(
|
||||||
|
"Invalid left-hand side parameter. Expected Var, Ctr or Num, got {:?}",
|
||||||
|
arg
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} 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);
|
funs.insert(kind_name, stmt);
|
||||||
} else {
|
} else {
|
||||||
unreachable!();
|
unreachable!("Expected list of Funs, found {:?}", stmt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let ctrs = file.ctrs;
|
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
|
let name = Name::from_str(&format!("{}.{}", name, used - 1)).unwrap(); // TODO: Think if this errs or not
|
||||||
Term::Var { name }
|
Term::Var { name }
|
||||||
} else {
|
} else {
|
||||||
unreachable!(); // Is it? Unbound variable
|
unreachable!("Unbound variable '{}' in kdl compilation", name.to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user