Skip to content
This repository was archived by the owner on Nov 6, 2022. It is now read-only.

Commit 116c71b

Browse files
some simplifications
1 parent 4f501eb commit 116c71b

File tree

6 files changed

+60
-123
lines changed

6 files changed

+60
-123
lines changed

src/compile/ast.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -362,25 +362,6 @@ impl Type {
362362
tipe
363363
}
364364

365-
/// Converts all named params to immutable generics. This ensures they are never changed again at call sites.
366-
pub fn commit_params(&self, generalization: &Vec<StringId>) -> Self {
367-
let slots = generalization
368-
.into_iter()
369-
.enumerate()
370-
.map(|(index, id)| (*id, index))
371-
.collect::<HashMap<_, _>>();
372-
373-
let mut tipe = self.clone();
374-
tipe.replace(&mut |tipe| {
375-
if let Type::Nominal(_, id, _) = tipe {
376-
if let Some(slot) = slots.get(&id) {
377-
*tipe = Type::Generic(*slot);
378-
}
379-
}
380-
});
381-
tipe
382-
}
383-
384365
/// If self is a Struct, and name is the StringID of a field of self, then returns Some(n), where
385366
/// n is the index of the field of self whose ID matches name. Otherwise returns None.
386367
pub fn get_struct_slot_by_name(&self, name: String) -> Option<usize> {
@@ -1851,12 +1832,12 @@ pub enum ExprKind {
18511832
Trinary(TrinaryOp, Box<Expr>, Box<Expr>, Box<Expr>),
18521833
ShortcutOr(Box<Expr>, Box<Expr>),
18531834
ShortcutAnd(Box<Expr>, Box<Expr>),
1854-
VariableRef(StringId),
1835+
VariableRef(StringId, Vec<Type>),
18551836
TupleRef(Box<Expr>, Uint256),
18561837
DotRef(Box<Expr>, String),
18571838
Constant(Constant),
18581839
OptionInitializer(Box<Expr>),
1859-
FunctionCall(Box<Expr>, Vec<Expr>, Vec<Type>),
1840+
FunctionCall(Box<Expr>, Vec<Expr>),
18601841
CodeBlock(CodeBlock),
18611842
ArrayOrMapRef(Box<Expr>, Box<Expr>),
18621843
StructInitializer(Vec<FieldInitializer>),

src/compile/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use std::io::{self, Read};
2626
use std::path::Path;
2727
use typecheck::TypeCheckedFunc;
2828

29-
3029
pub use ast::{DebugInfo, GlobalVar, StructField, TopLevelDecl, Type, TypeTree};
3130
pub use source::Lines;
3231
use std::str::FromStr;

src/compile/typecheck.rs

Lines changed: 46 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,43 +1187,6 @@ impl TypeCheckedExpr {
11871187
TypeCheckedExprKind::Loop(..) => Type::Every,
11881188
}
11891189
}
1190-
1191-
/// Extracts the type returned from the expression.
1192-
pub fn get_type_mut(&mut self) -> Option<&mut Type> {
1193-
Some(match &mut self.kind {
1194-
TypeCheckedExprKind::UnaryOp(_, _, t) => t,
1195-
TypeCheckedExprKind::Binary(_, _, _, t) => t,
1196-
TypeCheckedExprKind::Trinary(_, _, _, _, t) => t,
1197-
TypeCheckedExprKind::LocalVariableRef(.., t) => t,
1198-
TypeCheckedExprKind::GlobalVariableRef(.., t) => t,
1199-
TypeCheckedExprKind::FuncRef(.., t) => t,
1200-
TypeCheckedExprKind::TupleRef(.., t) => t,
1201-
TypeCheckedExprKind::DotRef(.., t) => t,
1202-
TypeCheckedExprKind::Const(.., t) => t,
1203-
TypeCheckedExprKind::FunctionCall(.., t, _) => t,
1204-
TypeCheckedExprKind::StructInitializer(.., t) => t,
1205-
TypeCheckedExprKind::ArrayRef(.., t) => t,
1206-
TypeCheckedExprKind::FixedArrayRef(.., t) => t,
1207-
TypeCheckedExprKind::MapRef(.., t) => t,
1208-
TypeCheckedExprKind::ClosureLoad(.., t) => t,
1209-
TypeCheckedExprKind::Tuple(.., t) => t,
1210-
TypeCheckedExprKind::NewArray(.., t) => t,
1211-
TypeCheckedExprKind::NewFixedArray(.., t) => t,
1212-
TypeCheckedExprKind::NewMap(t) => t,
1213-
TypeCheckedExprKind::ArrayMod(.., t) => t,
1214-
TypeCheckedExprKind::FixedArrayMod(.., t) => t,
1215-
TypeCheckedExprKind::MapMod(.., t) => t,
1216-
TypeCheckedExprKind::StructMod(.., t) => t,
1217-
TypeCheckedExprKind::Cast(.., t) => t,
1218-
TypeCheckedExprKind::Asm(t, ..) => t,
1219-
TypeCheckedExprKind::Try(.., t) => t,
1220-
TypeCheckedExprKind::If(.., t) => t,
1221-
TypeCheckedExprKind::IfLet(.., t) => t,
1222-
TypeCheckedExprKind::Variant(sub) => return sub.get_type_mut(), // be careful with these,
1223-
TypeCheckedExprKind::CodeBlock(block) => return block.get_type_mut(), // they are to options
1224-
_ => return None,
1225-
})
1226-
}
12271190
}
12281191

12291192
type TypeCheckedFieldInitializer = FieldInitializer<TypeCheckedExpr>;
@@ -2257,15 +2220,54 @@ fn typecheck_expr(
22572220
scopes,
22582221
)?)))
22592222
}
2260-
ExprKind::VariableRef(id) => {
2223+
ExprKind::VariableRef(id, spec) => {
22612224
if let Some(tipe) = func_table.get(id) {
2262-
let tipe = tipe.rep(type_tree)?;
2225+
let template_type = tipe.rep(type_tree)?;
2226+
let num_generic_params = tipe.count_generic_slots();
2227+
2228+
if spec.len() != num_generic_params {
2229+
return Err(CompileError::new(
2230+
"Generics error",
2231+
format!(
2232+
"Func {} has {} generic args but was passed {}",
2233+
Color::red(string_table.name_from_id(*id)),
2234+
Color::red(num_generic_params),
2235+
Color::red(spec.len()),
2236+
),
2237+
debug_info.locs(),
2238+
));
2239+
}
2240+
2241+
let tipe = template_type.make_specific(spec)?;
22632242
Ok(TypeCheckedExprKind::FuncRef(*id, tipe))
22642243
} else if let Some(tipe) = type_table.get(id) {
2244+
if !spec.is_empty() {
2245+
return Err(CompileError::new(
2246+
"Generics error",
2247+
format!(
2248+
"Variable {} doesn't need specialization",
2249+
Color::red(string_table.name_from_id(*id)),
2250+
),
2251+
debug_info.locs(),
2252+
));
2253+
}
2254+
22652255
let tipe = tipe.rep(type_tree)?;
22662256
Ok(TypeCheckedExprKind::LocalVariableRef(*id, tipe))
22672257
} else if let Some(tipe) = global_vars.get(id) {
22682258
let tipe = tipe.rep(type_tree)?;
2259+
2260+
if !spec.is_empty() {
2261+
return Err(CompileError::new(
2262+
"Generics error",
2263+
format!(
2264+
"Global variable {} doesn't need specialization",
2265+
Color::red(string_table.name_from_id(*id)),
2266+
),
2267+
debug_info.locs(),
2268+
));
2269+
}
2270+
22692271
Ok(TypeCheckedExprKind::GlobalVariableRef(*id, tipe))
22702272
} else {
22712273
Err(CompileError::new_type_error(
@@ -2372,8 +2374,8 @@ fn typecheck_expr(
23722374
Constant::Option(o) => TypeCheckedExprKind::Const(o.value(), o.type_of()),
23732375
Constant::Null => TypeCheckedExprKind::Const(Value::none(), Type::Any),
23742376
}),
2375-
ExprKind::FunctionCall(expr, args, spec) => {
2376-
let mut expr = typecheck_expr(
2377+
ExprKind::FunctionCall(expr, args) => {
2378+
let expr = typecheck_expr(
23772379
expr,
23782380
type_table,
23792381
global_vars,
@@ -2386,40 +2388,9 @@ fn typecheck_expr(
23862388
scopes,
23872389
)?;
23882390

2389-
let old_type = expr.get_type().rep(type_tree)?;
2390-
let num_generic_params = old_type.count_generic_slots();
2391+
let expr_type = expr.get_type().rep(type_tree)?;
23912392

2392-
if spec.len() != num_generic_params {
2393-
return Err(CompileError::new(
2394-
"Generics error",
2395-
format!(
2396-
"Func has {} generic args but was passed {}",
2397-
Color::red(num_generic_params),
2398-
Color::red(spec.len()),
2399-
),
2400-
debug_info.locs(),
2401-
));
2402-
}
2403-
2404-
match expr.get_type_mut() {
2405-
Some(tipe) => {
2406-
*tipe = tipe
2407-
.rep(type_tree)?
2408-
.make_specific(spec)?
2409-
.commit_params(&func.generics);
2410-
}
2411-
None => {
2412-
return Err(CompileError::new_type_error(
2413-
format!(
2414-
"tried to call a {}, which is not a function",
2415-
Color::red(old_type.print(type_tree)),
2416-
),
2417-
debug_info.locs(),
2418-
));
2419-
}
2420-
}
2421-
2422-
match expr.get_type().rep(type_tree)? {
2393+
match expr_type.clone() {
24232394
Type::Func(prop, arg_types, ret_type) => {
24242395
if args.len() == arg_types.len() {
24252396
let mut tc_args = Vec::new();
@@ -2439,7 +2410,6 @@ fn typecheck_expr(
24392410
tc_args.push(tc_arg);
24402411

24412412
let resolved_arg_type = arg_types[i].clone();
2442-
//let resolved_arg_type = resolved_arg_type.make_generic(&func.generics);
24432413

24442414
if !resolved_arg_type.assignable(
24452415
&tc_args[i].get_type().rep(type_tree)?,
@@ -2472,7 +2442,7 @@ fn typecheck_expr(
24722442
_ => Err(CompileError::new_type_error(
24732443
format!(
24742444
"tried to call a {}, which is not a function",
2475-
Color::red(old_type.print(type_tree))
2445+
Color::red(expr_type.print(type_tree))
24762446
),
24772447
debug_info.locs(),
24782448
)),
@@ -2495,12 +2465,6 @@ fn typecheck_expr(
24952465

24962466
// a closures inherits its parent's generics
24972467
closure_func.generics = func.generics.clone();
2498-
closure_func.tipe = closure_func.tipe.commit_params(&func.generics);
2499-
closure_func.ret_type = closure_func.ret_type.commit_params(&func.generics);
2500-
closure_func
2501-
.args
2502-
.iter_mut()
2503-
.for_each(|arg| arg.tipe = arg.tipe.commit_params(&func.generics));
25042468

25052469
let id = closure_func.id;
25062470
let tipe = closure_func.tipe.clone();
@@ -4372,12 +4336,6 @@ impl TypeCheckedCodeBlock {
43724336
.map(|r| r.get_type())
43734337
.unwrap_or(Type::Void)
43744338
}
4375-
pub fn get_type_mut(&mut self) -> Option<&mut Type> {
4376-
match &mut self.ret_expr {
4377-
Some(expr) => expr.get_type_mut(),
4378-
None => None,
4379-
}
4380-
}
43814339
}
43824340

43834341
impl AbstractSyntaxTree for TypeCheckedCodeBlock {

src/mini.lalrpop

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ StatementKind: StatementKind = {
248248

249249
// the withs we assign at the end
250250
let mut withs = Expr {
251-
kind: ExprKind::VariableRef(i),
251+
kind: ExprKind::VariableRef(i, vec![]),
252252
debug_info: debug_info,
253253
};
254254

@@ -261,7 +261,7 @@ StatementKind: StatementKind = {
261261
// a nested set of sub-value identifier ops that lets us build up nodes for those like a.b.c by going through
262262
// each of those for a, a.b, a.b.c, etc
263263
let mut nest = Expr {
264-
kind: ExprKind::VariableRef(i),
264+
kind: ExprKind::VariableRef(i, vec![]),
265265
debug_info: debug_info,
266266
};
267267

@@ -474,13 +474,11 @@ Expr10: Expr = {
474474
Expr11: Expr = {
475475
<lno: @L> <c: Const> => Expr { kind: ExprKind::Constant(c), debug_info: DebugInfo::from(file_info.location(BytePos::from(lno), filename))},
476476
<lno: @L> "Some(" <e: Expr> ")" => Expr { kind: ExprKind::OptionInitializer(Box::new(e)), debug_info: DebugInfo::from(file_info.location(BytePos::from(lno), filename))},
477-
<lno: @L> <callable: Expr11> <s:("::" <Specialization>)?> "(" <e: Expr> ")" => {
478-
let spec = s.into_iter().flatten().collect();
479-
Expr::lno(ExprKind::FunctionCall(Box::new(callable), vec![e], spec), file_info, lno, filename)
477+
<lno: @L> <callable: Expr11> "(" <e: Expr> ")" => {
478+
Expr::lno(ExprKind::FunctionCall(Box::new(callable), vec![e]), file_info, lno, filename)
480479
},
481-
<lno: @L> <callable: Expr11> <s:("::" <Specialization>)?> "(" <c: CommaedExprs?> ")" => {
482-
let spec = s.into_iter().flatten().collect();
483-
Expr::lno(ExprKind::FunctionCall(Box::new(callable), c.unwrap_or(vec![]), spec), file_info, lno, filename)
480+
<lno: @L> <callable: Expr11> "(" <c: CommaedExprs?> ")" => {
481+
Expr::lno(ExprKind::FunctionCall(Box::new(callable), c.unwrap_or(vec![])), file_info, lno, filename)
484482
},
485483
<lno: @L> <e1:Expr11> "[" <e2:Expr> "]" => Expr {
486484
kind: ExprKind::ArrayOrMapRef(Box::new(e1), Box::new(e2)),
@@ -564,7 +562,10 @@ Expr13: Expr = {
564562
<lno: @L> "error" => Expr { kind: ExprKind::Error, debug_info: DebugInfo::from(file_info.location(BytePos::from(lno), filename))},
565563
<lno: @L> "getGas" "(" ")" => Expr { kind: ExprKind::GetGas, debug_info: DebugInfo::from(file_info.location(BytePos::from(lno), filename))},
566564
<lno: @L> "setGas" "(" <e:Expr> ")" => Expr { kind: ExprKind::SetGas(Box::new(e)), debug_info: DebugInfo::from(file_info.location(BytePos::from(lno), filename))},
567-
<lno: @L> <i: Ident> => Expr::lno(ExprKind::VariableRef(i), file_info, lno, filename),
565+
<lno: @L> <i: Ident> <s:("::" <Specialization>)?> => {
566+
let spec = s.into_iter().flatten().collect();
567+
Expr::lno(ExprKind::VariableRef(i, spec), file_info, lno, filename)
568+
},
568569
<lno: @L> <q: QuoteString> => Expr::lno(ExprKind::Quote(q), file_info, lno, filename),
569570
}
570571

stdlib2/array.mini

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ public func array_new<T>(size: uint, base_val: T) -> array<T> {
2121
size: size,
2222
topstep: chunk,
2323
contents: newfixedarray(8, base_val),
24-
access: closure(arr: array<T>, index: uint) -> T {
25-
return array_get::<T>(arr, index);
26-
},
24+
access: array_get::<T>,
2725
};
2826
}
2927

stdlib2/priorityqtest.mini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std2::priorityq::priorityq_get;
99

1010

1111
write func main() {
12-
asm(tests().1) { log };
12+
asm(tests().1) { log };
1313
}
1414

1515
func tests() -> string {

0 commit comments

Comments
 (0)