fix: Format and rename some things

This commit is contained in:
Nicolas Abril 2022-11-26 16:41:34 +01:00
parent 30611936cd
commit 85bb2d0655
10 changed files with 103 additions and 76 deletions

View File

@ -99,10 +99,7 @@ fn range_to_num(range: Range) -> Box<Term> {
fn set_origin(ident: &Ident) -> Box<Term> {
mk_lifted_ctr(
"Kind.Term.set_origin".to_owned(),
vec![
range_to_num(ident.range),
mk_var(ident.to_str()),
],
vec![range_to_num(ident.range), mk_var(ident.to_str())],
)
}
@ -151,10 +148,16 @@ fn codegen_all_expr(
) -> Box<Term> {
use kind_tree::desugared::ExprKind::*;
match &expr.data {
Typ => mk_lifted_ctr(eval_ctr(quote, TermTag::Typ), vec![range_to_num(expr.range)]),
Typ => mk_lifted_ctr(
eval_ctr(quote, TermTag::Typ),
vec![range_to_num(expr.range)],
),
NumType {
typ: kind_tree::NumType::U60,
} => mk_lifted_ctr(eval_ctr(quote, TermTag::U60), vec![range_to_num(expr.range)]),
} => mk_lifted_ctr(
eval_ctr(quote, TermTag::U60),
vec![range_to_num(expr.range)],
),
NumType {
typ: kind_tree::NumType::U120,
} => mk_lifted_ctr(
@ -325,7 +328,10 @@ fn codegen_all_expr(
vec![range_to_num(expr.range), mk_u60(*num)],
),
Str { val } => codegen_all_expr(lhs_rule, lhs, num, quote, &desugar_str(val, expr.range)),
Hlp(_) => mk_lifted_ctr(eval_ctr(quote, TermTag::Hlp), vec![range_to_num(expr.range)]),
Hlp(_) => mk_lifted_ctr(
eval_ctr(quote, TermTag::Hlp),
vec![range_to_num(expr.range)],
),
Err => panic!("Internal Error: Was not expecting an ERR node inside the HVM checker"),
}
}

View File

@ -2,15 +2,12 @@ use checker::eval;
use errors::DriverError;
use fxhash::FxHashSet;
use kind_pass::{desugar, erasure, expand};
use kind_report::{data::Diagnostic, report::FileCache};
use kind_report::report::FileCache;
use kind_span::SyntaxCtxIndex;
use kind_tree::{backend, concrete, desugared, untyped};
use session::Session;
use std::{
path::{Path, PathBuf},
sync::mpsc::Sender,
};
use std::path::PathBuf;
use kind_checker as checker;

View File

@ -511,7 +511,12 @@ impl<'a> ErasureState<'a> {
Box::new(untyped::Entry {
name: entry.name.clone(),
args: entry.args.iter().filter(|x| !x.erased).map(|x| (x.name.to_string(), x.range, false)).collect(),
args: entry
.args
.iter()
.filter(|x| !x.erased)
.map(|x| (x.name.to_string(), x.range, false))
.collect(),
rules,
attrs: entry.attrs.clone(),
range: entry.range,

View File

@ -17,6 +17,7 @@ pub fn compile_book(book: untyped::Book) -> File {
}
pub fn compile_term(expr: &untyped::Expr) -> Box<Term> {
use kind_tree::Number;
use untyped::ExprKind::*;
match &expr.data {
Var { name } => Box::new(Term::Var {
@ -41,10 +42,14 @@ pub fn compile_term(expr: &untyped::Expr) -> Box<Term> {
expr: compile_term(val),
body: compile_term(next),
}),
Num { num: kind_tree::Number::U60(numb) } => Box::new(Term::U6O {
Num {
num: Number::U60(numb),
} => Box::new(Term::U6O {
numb: u60::new(*numb),
}),
Num { num: kind_tree::Number::U120(numb) } => {
Num {
num: Number::U120(numb),
} => {
let hi = Box::new(Term::U6O {
numb: u60::new((numb >> 60) as u64),
});

View File

@ -145,51 +145,51 @@ pub fn err_term() -> kindelia_lang::ast::Term {
}
pub fn compile_expr(ctx: &mut CompileCtx, expr: &untyped::Expr) -> kindelia_lang::ast::Term {
use crate::untyped::ExprKind::*;
use kdl::Term as T;
use crate::untyped::ExprKind as From;
use kdl::Term as To;
match &expr.data {
App { fun, args } => {
From::App { fun, args } => {
let mut expr = compile_expr(ctx, fun);
for binding in args {
let body = compile_expr(ctx, &binding);
expr = T::App {
expr = To::App {
func: Box::new(expr),
argm: Box::new(body),
};
}
expr
}
Binary { op, left, right } => {
From::Binary { op, left, right } => {
// TODO: Special compilation for U60 ops
let oper = compile_oper(op);
let val0 = Box::new(compile_expr(ctx, left));
let val1 = Box::new(compile_expr(ctx, right));
T::Op2 { oper, val0, val1 }
To::Op2 { oper, val0, val1 }
}
Ctr { name, args } => {
From::Ctr { name, args } => {
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));
}
T::Ctr {
To::Ctr {
name,
args: new_args,
}
}
Fun { name, args } => {
From::Fun { name, args } => {
// TODO: Special compilation for U60 and U120 ops
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));
}
T::Fun {
To::Fun {
name,
args: new_args,
}
}
Lambda {
From::Lambda {
param,
body,
erased: _,
@ -197,44 +197,44 @@ pub fn compile_expr(ctx: &mut CompileCtx, expr: &untyped::Expr) -> kindelia_lang
let name = kdl::Name::from_str(param.to_str());
if let Ok(name) = name {
let body = Box::new(compile_expr(ctx, &body));
T::Lam { name, body }
To::Lam { name, body }
} else {
ctx.send_err(Box::new(KdlError::InvalidVarName(param.range)));
err_term()
}
}
Let { name, val, next } => {
From::Let { name, val, next } => {
let res_name = kdl::Name::from_str(name.to_str());
if let Ok(name) = res_name {
let expr = Box::new(compile_expr(ctx, &val));
let func = Box::new(T::Lam { name, body: expr });
let func = Box::new(To::Lam { name, body: expr });
let argm = Box::new(compile_expr(ctx, next));
T::App { func, argm }
To::App { func, argm }
} else {
ctx.send_err(Box::new(KdlError::InvalidVarName(name.range)));
err_term()
}
}
Num {
From::Num {
num: Number::U60(numb),
} => T::Num {
} => To::Num {
numb: kdl::U120(*numb as u128),
},
Num {
From::Num {
num: Number::U120(numb),
} => T::Num {
} => To::Num {
numb: kdl::U120(*numb),
},
Var { name } => {
From::Var { name } => {
let res_name = kdl::Name::from_str(name.to_str());
if let Ok(name) = res_name {
T::Var { name }
To::Var { name }
} else {
ctx.send_err(Box::new(KdlError::InvalidVarName(name.range)));
err_term()
}
}
Str { val } => {
From::Str { val } => {
let nil = kdl::Term::Ctr {
name: ctx.kdl_names.get("String.nil").unwrap().clone(),
args: vec![],
@ -254,7 +254,7 @@ pub fn compile_expr(ctx: &mut CompileCtx, expr: &untyped::Expr) -> kindelia_lang
val.chars().rfold(nil, |rest, chr| cons(chr as u128, rest))
}
Err => unreachable!("Should not have errors inside generation"),
From::Err => unreachable!("Should not have errors inside generation"),
}
}
@ -347,24 +347,24 @@ impl Display for File {
}
fn compile_oper(oper: &kind_tree::Operator) -> kdl::Oper {
use kdl::Oper as T;
use kind_tree::Operator as F;
use kdl::Oper as To;
use kind_tree::Operator as From;
match oper {
F::Add => T::Add,
F::Sub => T::Sub,
F::Mul => T::Mul,
F::Div => T::Div,
F::Mod => T::Mod,
F::Shl => T::Shl,
F::Shr => T::Shr,
F::Eql => T::Eql,
F::Neq => T::Neq,
F::Ltn => T::Ltn,
F::Lte => T::Lte,
F::Gte => T::Gte,
F::Gtn => T::Gtn,
F::And => T::And,
F::Xor => T::Xor,
F::Or => T::Or,
From::Add => To::Add,
From::Sub => To::Sub,
From::Mul => To::Mul,
From::Div => To::Div,
From::Mod => To::Mod,
From::Shl => To::Shl,
From::Shr => To::Shr,
From::Eql => To::Eql,
From::Neq => To::Neq,
From::Ltn => To::Ltn,
From::Lte => To::Lte,
From::Gte => To::Gte,
From::Gtn => To::Gtn,
From::And => To::And,
From::Xor => To::Xor,
From::Or => To::Or,
}
}

View File

@ -1,4 +1,4 @@
use kind_report::data::{Diagnostic, DiagnosticFrame, Severity, Marker, Color};
use kind_report::data::{Color, Diagnostic, DiagnosticFrame, Marker, Severity};
use kind_span::Range;
pub enum KdlError {
@ -15,7 +15,6 @@ impl Diagnostic for KdlError {
KdlError::ShouldNotHaveArguments(range) => Some(range.ctx),
KdlError::ShouldHaveOnlyOneRule(range) => Some(range.ctx),
KdlError::NoInitEntry(range) => Some(range.ctx),
}
}
@ -79,4 +78,4 @@ impl Diagnostic for KdlError {
},
}
}
}
}

View File

@ -1,7 +1,7 @@
use fxhash::{FxHashMap, FxHashSet};
use kind_span::Range;
use kind_tree::symbol::{Ident, QualifiedIdent};
use kind_tree::untyped::{self, Entry, Expr, ExprKind, Rule, Book};
use kind_tree::untyped::{self, Book, Entry, Expr, ExprKind, Rule};
use linked_hash_map::LinkedHashMap;
use crate::subst::subst;
@ -94,7 +94,9 @@ fn split_rule(
Expr::var(name)
}
ExprKind::Var { .. } => field.clone(),
_ => panic!("Internal Error: Cannot use this kind of expression during flattening"),
_ => panic!(
"Internal Error: Cannot use this kind of expression during flattening"
),
};
new_pat_args.push(arg.clone());
old_rule_body_args.push(arg);
@ -170,7 +172,9 @@ fn split_rule(
assert!(!new_entry_rules.is_empty());
let new_entry_args = (0..new_entry_rules[0].pats.len()).map(|n| (format!("x{}", n), Range::ghost_range(), false)).collect();
let new_entry_args = (0..new_entry_rules[0].pats.len())
.map(|n| (format!("x{}", n), Range::ghost_range(), false))
.collect();
let new_entry = Entry {
name: new_entry_name,
@ -186,7 +190,7 @@ fn split_rule(
fn flatten_entry(entry: &Entry) -> Vec<Entry> {
let mut name_count = 0;
let mut skip: FxHashSet<usize> = FxHashSet::default();
let mut new_entries: Vec<Entry> = Vec::new();
let mut old_entry_rules: Vec<Rule> = Vec::new();
@ -195,7 +199,8 @@ fn flatten_entry(entry: &Entry) -> Vec<Entry> {
if !skip.contains(&i) {
let rule = &entry.rules[i];
if must_split(rule) {
let (old_rule, split_entries) = split_rule(rule, &entry, i, &mut name_count, &mut skip);
let (old_rule, split_entries) =
split_rule(rule, &entry, i, &mut name_count, &mut skip);
old_entry_rules.push(old_rule);
new_entries.extend(split_entries);
} else {
@ -216,7 +221,7 @@ fn flatten_entry(entry: &Entry) -> Vec<Entry> {
new_entries
}
pub fn flatten(book: untyped::Book) -> untyped::Book {
pub fn flatten(book: untyped::Book) -> untyped::Book {
let mut book = book;
let mut names = FxHashMap::default();
let mut entrs = LinkedHashMap::default();
@ -229,7 +234,9 @@ pub fn flatten(book: untyped::Book) -> untyped::Book {
}
}
let book = Book { names, entrs, holes: book.holes };
book
Book {
names,
entrs,
holes: book.holes,
}
}

View File

@ -2,17 +2,26 @@ use std::sync::mpsc::Sender;
use flatten::flatten;
use kind_report::data::Diagnostic;
use kind_tree::{untyped};
use kind_tree::untyped;
pub use compile::File;
mod compile;
mod errors;
mod flatten;
mod linearize;
mod subst;
mod errors;
pub fn compile_book(book: untyped::Book, sender: Sender<Box<dyn Diagnostic>>, namespace: &str) -> Option<compile::File> {
pub fn compile_book(
book: untyped::Book,
sender: Sender<Box<dyn Diagnostic>>,
namespace: &str,
) -> Option<compile::File> {
// TODO: Remove kdl_states (maybe check if they're ever called?)
// TODO: Don't erase kdl_state functions
// TODO: Convert to some sort of Kindelia.Contract
let flattened = flatten(book);
let file = compile::compile_book(&flattened, sender, namespace)?;
let file = linearize::linearize_file(file);
Some(file)
}

View File

@ -1,4 +1,4 @@
use kind_tree::{untyped::Expr, symbol::Ident};
use kind_tree::{symbol::Ident, untyped::Expr};
pub fn subst(term: &mut Expr, from: &Ident, to: &Expr) {
use kind_tree::untyped::ExprKind::*;
@ -24,19 +24,19 @@ pub fn subst(term: &mut Expr, from: &Ident, to: &Expr) {
subst(next, from, to);
}
}
Binary { op: _, left, right } => {
subst(left, from, to);
subst(right, from, to);
}
Lambda { param, body, .. } if param.to_str() != from.to_str() => subst(body, from, to),
Num { .. } => (),
Str { .. } => (),
Var { .. } => (),
Lambda { .. } => (),
Err => unreachable!("Err should not be used inside the compiledr"),
}
}

View File

@ -1 +0,0 @@