Skip to content

Commit c275a01

Browse files
committed
[WIP] Lushui panic is not Rust panic
1 parent 87b47f3 commit c275a01

File tree

9 files changed

+37
-11
lines changed

9 files changed

+37
-11
lines changed

compiler/codegen_llvm/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ impl<'a, 'ctx> Generator<'a, 'ctx> {
294294
}
295295
Substituted(_) => todo!(),
296296
IO(_) => todo!(),
297+
Panic(_) => todo!(),
297298
Error => todo!(),
298299
}
299300
}
@@ -341,6 +342,7 @@ impl<'a, 'ctx> Generator<'a, 'ctx> {
341342
IntrinsicApplication(_) => todo!(),
342343
Projection(_) => todo!(),
343344
IO(_) => todo!(),
345+
Panic(_) => todo!(),
344346
// @Question how should we handle that?
345347
Error => todo!(),
346348
};
@@ -407,6 +409,7 @@ impl<'a, 'ctx> Generator<'a, 'ctx> {
407409
IntrinsicApplication(_) => todo!(),
408410
Projection(_) => todo!(),
409411
IO(_) => todo!(),
412+
Panic(_) => todo!(),
410413
Error => todo!(),
411414
}
412415
}
@@ -451,6 +454,7 @@ impl ExpressionExt for hir::Expression {
451454
Substituted(_) => todo!(),
452455
Projection(_) => todo!(),
453456
IO(_) => todo!(),
457+
Panic(_) => todo!(),
454458
Error => todo!(),
455459
}
456460
}

compiler/hir/src/interfaceable.rs

+5
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,15 @@ pub enum Value {
3232
type_: Type,
3333
value: Option<Box<Value>>,
3434
},
35+
// @Task rename to Effect
3536
IO {
3637
index: usize,
3738
arguments: Vec<Value>,
3839
},
40+
// @Task make this an effect
41+
Panic {
42+
message: String,
43+
},
3944
Opaque(()),
4045
}
4146

compiler/hir/src/intrinsic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ pub fn functions() -> HashMap<Function, FunctionValue> {
165165
);
166166
intrinsics.insert(
167167
Panic,
168-
pure!(|_type: Opaque, message: Text| panic!("{message}")),
168+
pure!(|_type: Opaque, message: Text| Value::Panic { message }),
169169
);
170170

171171
intrinsics

compiler/hir/src/lib.rs

+15
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ pub enum BareExpression {
118118
Substituted(Box<Substituted>),
119119
IntrinsicApplication(Box<IntrinsicApplication>),
120120
Projection(Box<Projection>),
121+
// @Task rename to Effect
121122
IO(Box<IO>),
123+
// @Task make this an effect
124+
Panic(Box<Panic>),
122125
Error,
123126
}
124127

@@ -249,6 +252,18 @@ impl From<IO> for BareExpression {
249252
}
250253
}
251254

255+
#[derive(Clone, Debug)]
256+
// @Temporary
257+
pub struct Panic {
258+
pub message: String,
259+
}
260+
261+
impl From<Panic> for BareExpression {
262+
fn from(panic: Panic) -> Self {
263+
Self::Panic(Box::new(panic))
264+
}
265+
}
266+
252267
#[derive(Clone, Debug)]
253268
pub struct Case {
254269
pub pattern: Pattern,

compiler/hir_format/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ fn write_lower_expression(
243243
}
244244
write!(f, ")")
245245
}
246+
// @Temporary, @Task just write it out as an application of `panic`
247+
Panic(panic) => write!(f, "?(panic {:?})", panic.message),
246248
Substituted(substituted) => {
247249
write!(f, "?(substituted ",)?;
248250
substituted.substitution.write(context, f)?;

compiler/server/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ impl FindBinding for hir::Expression {
474474
IntrinsicApplication(_) => None, // @Task
475475
Projection(_) => None, // @Task
476476
IO(_) => None, // @Task
477+
Panic(_) => None, // @Task
477478
Error => None, // @Task
478479
}
479480
}

compiler/session/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,9 @@ impl InterfaceableBindingExt for interfaceable::Value {
472472
}
473473
.into(),
474474
),
475+
Self::Panic { message } => {
476+
Expression::new(default(), default(), hir::Panic { message }.into())
477+
}
475478
Self::Opaque(()) => unreachable!(),
476479
})
477480
}

compiler/typer/src/interpreter.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl<'a> Interpreter<'a> {
9393
// @Task verify
9494
(Projection(_), _) => expression,
9595
// @Temporary
96-
(IO(_), _) => expression,
96+
(IO(_), _) | (Panic(_), _) => expression,
9797
(Application(application), substitution) => Expression::new(
9898
expression.attributes,
9999
expression.span,
@@ -415,6 +415,9 @@ impl<'a> Interpreter<'a> {
415415
}
416416
}
417417
Type | Number(_) | Text(_) | IO(_) => expression,
418+
// @Task smh add a backtrace here!
419+
// @Task make sure that ?(panic _) is evaluated *eagerly* not lazily!
420+
Panic(_) => expression,
418421
Projection(_) => todo!(),
419422
PiType(pi) => match context.form {
420423
Form::Normal => {
@@ -616,34 +619,25 @@ impl<'a> Interpreter<'a> {
616619
binder: Identifier,
617620
arguments: Vec<Expression>,
618621
) -> Result<Option<Expression>> {
619-
eprintln!("apply_intrinsic_function binder={binder} args=[{arguments:?}]");
620-
621622
match self.look_up(binder.declaration_index().unwrap()).kind {
622623
hir::EntityKind::IntrinsicFunction {
623624
arity, function, ..
624625
} => Ok(if arguments.len() == arity {
625-
eprintln!(" look_up -> [entity] intrinsic function");
626-
627626
let mut value_arguments = Vec::new();
628627

629628
// @Task tidy up with iterator combinators
630629
for argument in arguments {
631-
dbg!(&argument);
632-
633630
if let Some(argument) =
634631
interfaceable::Value::from_expression(&argument, self.session)
635632
{
636-
eprintln!("interfaceable argument found, pushing ...");
637633
value_arguments.push(argument);
638634
} else {
639-
eprintln!("non-interfaceable argument found, aborting ...");
640635
return Ok(None);
641636
}
642637
}
643638

644639
Some(function(value_arguments).into_expression(self.session)?)
645640
} else {
646-
eprintln!(" look_up -> None");
647641
None
648642
}),
649643
_ => unreachable!(),

compiler/typer/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,8 @@ expected type ‘_ -> _’
768768
IO(_) => self
769769
.session
770770
.look_up_intrinsic_type(intrinsic::Type::IO, Some(expression.span))?,
771+
// @Task this should be of any type, it is should be a (flexible) inference variable
772+
Panic(_) => todo!(),
771773
Error => expression,
772774
})
773775
}

0 commit comments

Comments
 (0)