Skip to content

Commit c957e55

Browse files
committed
2 parents 45050ce + bd9a17c commit c957e55

File tree

8 files changed

+41
-51
lines changed

8 files changed

+41
-51
lines changed

src/frontend/parse.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ struct FrontendParser;
1313

1414
#[derive(Clone, Debug)]
1515
pub enum Statement {
16-
Match(Expr, Vec<(Pattern, Self)>),
1716
Let(Vec<(String, Option<Type>, Expr)>),
1817
Assign(Expr, Option<Box<dyn AssignOp + 'static>>, Expr),
1918
If(Expr, Box<Self>, Option<Box<Self>>),
@@ -36,12 +35,6 @@ impl Statement {
3635
// }, rest_expr])
3736

3837
let stmt = match (self, rest.clone()) {
39-
(Self::Match(e, arms), _) => Expr::Match(
40-
Box::new(e),
41-
arms.into_iter()
42-
.map(|(a, b)| (a, b.to_expr(None)))
43-
.collect(),
44-
),
4538
(Self::Assign(lhs, op, rhs), _) => {
4639
match op {
4740
Some(op) => lhs.refer().assign(op, rhs),

src/lir/compile.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ impl Compile for Expr {
458458
Self::EnumUnion(mut t, variant, val) => {
459459
// Get the size of the tagged union.
460460
let result_size = t.get_size(env)?;
461-
t = t.simplify_until_matches(env, Type::EnumUnion(BTreeMap::new()), |t, env| {
461+
t = t.simplify_until_matches(env, Type::EnumUnion(BTreeMap::new()), |t, _env| {
462462
Ok(!matches!(
463463
t,
464464
Type::Let(_, _, _) | Type::Symbol(_) | Type::Apply(_, _) | Type::Poly(_, _)
@@ -522,10 +522,20 @@ impl Compile for Expr {
522522
.as_type(Type::Pointer(elem.clone()))
523523
// Index the new pointer
524524
.idx(*idx.clone());
525-
// Push to the stack
526-
// if optimized_idx.type_check(env).is_ok() {
527-
// return optimized_idx.compile_expr(env, output);
528-
// }
525+
// The optimized index *may not be possible* if the array is
526+
// not able to be referenced (like an array literal). Type checking
527+
// a reference operation will confirm that the array is able to be
528+
// referenced. If the array is not a:
529+
// 1. Dereference
530+
// 2. Index access
531+
// 3. Member access
532+
// 4. Variable
533+
// If the array is a literal, then we will have to push the array
534+
// on the stack and essentially do the same address calculations,
535+
// but the difference is that the operation is not in-place.
536+
if optimized_idx.type_check(env).is_ok() {
537+
return optimized_idx.compile_expr(env, output);
538+
}
529539

530540
// Get the size of the element we will return.
531541
let elem_size = elem.get_size(env)?;

src/lir/expr/const_expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::lir::{
1515
};
1616

1717
use core::fmt;
18-
use std::collections::{BTreeMap, HashMap};
18+
use std::collections::BTreeMap;
1919

2020
/// A compiletime expression.
2121
#[derive(Clone, Debug, PartialEq)]

src/lir/expr/procedure/poly.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl PolyProcedure {
7575
})
7676
.collect::<Result<Vec<_>, Error>>()?;
7777

78-
let bind_type_args = |mut ty: Type| -> Result<Type, Error> {
78+
let bind_type_args = |ty: Type| -> Result<Type, Error> {
7979
// for (name, arg) in self.ty_params.iter().zip(ty_args.iter()) {
8080
// // ty = Type::let_bind(name, if arg == &Type::Symbol(name.clone()) {
8181
// // Type::Unit(name.clone(), Box::new(Type::Any))

src/lir/types/check.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,13 @@ impl TypeCheck for Expr {
493493
Self::Refer(e) => match *e.clone() {
494494
Expr::ConstExpr(ConstExpr::Symbol(_))
495495
| Expr::Deref(_)
496-
| Expr::Member(_, _)
497496
| Expr::Index(_, _) => e.type_check(env),
497+
Expr::Member(inner, _) => {
498+
// Confirm that the inner expression can be referenced.
499+
inner.refer().type_check(env)?;
500+
// If so, then make sure the expression being accessed is also sound.
501+
e.type_check(env)
502+
},
498503
other => Err(Error::InvalidRefer(other.clone())),
499504
},
500505
// Typecheck a dereference of a pointer.
@@ -912,8 +917,8 @@ impl TypeCheck for ConstExpr {
912917
cast_ty.clone(),
913918
));
914919
}
915-
// If it is, return success.
916-
Ok(())
920+
// If it is, return the result of the inner expression's typechecking result.
921+
expr.type_check(env)
917922
}
918923

919924
// Get the size of an expression in cells.

src/lir/types/mod.rs

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,10 @@ impl Type {
140140
}
141141
}
142142

143-
// pub fn contains(&self,)
144-
145-
/// Simplify an expression until it matches a given function.
143+
/// Simplify an expression until it matches a given function which "approves" of a type.
144+
/// This will perform template applications to simplify the type if possible as well.
145+
///
146+
/// This is the **prefered** way to simplify a type into a concrete type.
146147
pub fn simplify_until_matches(
147148
self,
148149
env: &Env,
@@ -156,11 +157,7 @@ impl Type {
156157
if f(&simplified, env)? || simplified.is_atomic() {
157158
return Ok(simplified);
158159
}
159-
simplified = simplified.simplify(env)?.perform_template_applications(
160-
env,
161-
&mut HashMap::new(),
162-
0,
163-
)?
160+
simplified = simplified.simplify(env)?.perform_template_applications(env, &mut HashMap::new())?
164161
}
165162
Err(Error::CouldntSimplify(simplified, expected))
166163
}
@@ -476,7 +473,6 @@ impl Type {
476473
&self,
477474
env: &Env,
478475
previous_applications: &mut HashMap<(Type, Vec<Type>), Type>,
479-
i: usize,
480476
) -> Result<Self, Error> {
481477
// If the type is an Apply on a Poly, then we can perform the application.
482478
// First, perform the applications on the type arguments.
@@ -497,25 +493,19 @@ impl Type {
497493

498494
match poly {
499495
Self::Poly(params, template) => {
500-
let save = template.clone();
501496
let mut template = *template;
502497
for (param, ty_arg) in params.iter().zip(ty_args.iter()) {
503498
template = template.substitute(param, ty_arg);
504499
}
505-
// template = template.simplify(env)?;
506-
// previous_applications.insert((Self::Poly(params, save), ty_args), template.clone());
507500
Ok(template)
508501
}
509502
Self::Symbol(s) => {
510503
match env.get_type(s.as_str()).cloned() {
511504
Some(Self::Poly(params, template)) => {
512-
let save = template.clone();
513505
let mut template = *template;
514506
for (param, ty_arg) in params.iter().zip(ty_args.iter()) {
515507
template = template.substitute(param, ty_arg);
516508
}
517-
// template = template.simplify(env)?;
518-
// previous_applications.insert((Self::Poly(params, save), ty_args), template.clone());
519509
Ok(template)
520510
}
521511
Some(other) => Ok(Self::Apply(Box::new(other), ty_args)),
@@ -771,14 +761,12 @@ impl Type {
771761
}
772762

773763
(Self::Apply(poly, ty_args), b) | (b, Self::Apply(poly, ty_args)) => {
774-
let a = Self::Apply(poly.clone(), ty_args.clone());
775764
// If the type is a polymorphic type, then we need to simplify it first.
776765
let f = poly.clone().simplify_until_matches(
777766
env,
778767
Type::Poly(vec![], Box::new(Type::Any)),
779-
|t, env| Ok(matches!(t, Type::Poly(_, _))),
768+
|t, _env| Ok(matches!(t, Type::Poly(_, _))),
780769
)?;
781-
// let a = a.simplify_until_matches(env, Type::Any, |t, env| Ok(t.is_simple()))?;
782770

783771
match f {
784772
Self::Poly(ty_params, mut template) => {
@@ -789,12 +777,6 @@ impl Type {
789777
}
790778
template.equals_checked(b, compared_symbols, env, i)?
791779
}
792-
// // Self::Symbol(_) => {
793-
// // // If the type is not a polymorphic type, then we can compare it to the
794-
// // // other type.
795-
// // // return a.equals_checked(b, compared_symbols, env, i);
796-
// // false
797-
// // }
798780
_ => {
799781
// If the type is not a polymorphic type, then we can compare it to the
800782
// other type.
@@ -873,7 +855,7 @@ impl Type {
873855
Type::Apply(_, _) | Type::Poly(_, _) => {
874856
let t = self
875857
.clone()
876-
.simplify_until_matches(env, Type::Any, |t, env| {
858+
.simplify_until_matches(env, Type::Any, |t, _env| {
877859
Ok(!matches!(
878860
t,
879861
Type::Let(_, _, _)
@@ -958,7 +940,7 @@ impl Type {
958940
Type::Apply(_, _) | Type::Poly(_, _) => {
959941
let t = self
960942
.clone()
961-
.simplify_until_matches(env, Type::Any, |t, env| {
943+
.simplify_until_matches(env, Type::Any, |t, _env| {
962944
Ok(!matches!(
963945
t,
964946
Type::Let(_, _, _)
@@ -1179,7 +1161,7 @@ impl fmt::Display for Type {
11791161
}
11801162

11811163
Self::Symbol(name) => write!(f, "{name}"),
1182-
Self::Unit(unit_name, ty) => write!(f, "unit {unit_name}"),
1164+
Self::Unit(unit_name, _ty) => write!(f, "unit {unit_name}"),
11831165
Self::Let(name, ty, ret) => write!(f, "let {name} = {ty} in {ret}"),
11841166
}
11851167
}

src/lir/types/size.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,15 @@ impl GetSize for Type {
150150
}
151151

152152
// Get the size of an `Apply` type.
153-
Self::Apply(poly, ty_args) => {
153+
Self::Apply(poly, _ty_args) => {
154154
if let Ok(size) = poly.get_size_checked(env, i) {
155155
return Ok(size);
156156
}
157157

158158
let result = self.clone().simplify_until_matches(
159159
env,
160160
Type::Poly(vec![], Box::new(Type::Any)),
161-
|t, env| Ok(t.is_simple()),
161+
|t, _env| Ok(t.is_simple()),
162162
)?;
163163

164164
result.get_size_checked(env, i)?

src/targets/x86.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl Architecture for X86 {
195195
})
196196
}
197197

198-
fn end(&mut self, matching: &CoreOp, fun: Option<usize>) -> String {
198+
fn end(&mut self, matching: &CoreOp, _fun: Option<usize>) -> String {
199199
match matching {
200200
CoreOp::Function => {
201201
let label = self.branch_match.pop().unwrap();
@@ -213,7 +213,7 @@ impl Architecture for X86 {
213213
}
214214
}
215215

216-
fn declare_proc(&mut self, label_id: usize) -> String {
216+
fn declare_proc(&mut self, _label_id: usize) -> String {
217217
let result = format!("movq $fun{fun_count}, funs+{}(%rip)\n{indent}jmp fun_end{fun_count}\nfun{fun_count}:\n{indent}pushq %rbp\n{indent}movq %rsp, %rbp\n", self.fun_count * 8, fun_count = self.fun_count, indent = self.indentation().unwrap());
218218
self.branch_match
219219
.push(format!("fun_end{fun_count}", fun_count = self.fun_count));
@@ -278,15 +278,15 @@ addq $8, %rsp # Deallocate the space on the stack",
278278
// Ok("*ptr = reg;".to_string())
279279
todo!()
280280
}
281-
fn prelude(&self, is_core: bool) -> Option<String> {
282-
let mut result = ".text
281+
fn prelude(&self, _is_core: bool) -> Option<String> {
282+
let result = ".text
283283
.globl main
284284
main:
285285
";
286286
Some(result.to_string())
287287
}
288288

289-
fn post_funs(&self, funs: Vec<i32>) -> Option<String> {
289+
fn post_funs(&self, _funs: Vec<i32>) -> Option<String> {
290290
None
291291
}
292292

0 commit comments

Comments
 (0)