Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panicking #163

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/codegen_llvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ impl<'a, 'ctx> Generator<'a, 'ctx> {
}
Substituted(_) => todo!(),
IO(_) => todo!(),
Panic(_) => todo!(),
Error => todo!(),
}
}
Expand Down Expand Up @@ -341,6 +342,7 @@ impl<'a, 'ctx> Generator<'a, 'ctx> {
IntrinsicApplication(_) => todo!(),
Projection(_) => todo!(),
IO(_) => todo!(),
Panic(_) => todo!(),
// @Question how should we handle that?
Error => todo!(),
};
Expand Down Expand Up @@ -407,6 +409,7 @@ impl<'a, 'ctx> Generator<'a, 'ctx> {
IntrinsicApplication(_) => todo!(),
Projection(_) => todo!(),
IO(_) => todo!(),
Panic(_) => todo!(),
Error => todo!(),
}
}
Expand Down Expand Up @@ -451,6 +454,7 @@ impl ExpressionExt for hir::Expression {
Substituted(_) => todo!(),
Projection(_) => todo!(),
IO(_) => todo!(),
Panic(_) => todo!(),
Error => todo!(),
}
}
Expand Down
17 changes: 17 additions & 0 deletions compiler/hir/src/interfaceable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use utilities::{Int, Nat};

/// An “interfaceable” type (i.e. an intrinsic or known type).
pub enum Type {
Void,
Unit,
Bool,
Nat,
Expand Down Expand Up @@ -31,10 +32,16 @@ pub enum Value {
type_: Type,
value: Option<Box<Value>>,
},
// @Task rename to Effect
IO {
index: usize,
arguments: Vec<Value>,
},
// @Task make this an effect
Panic {
message: String,
},
Opaque(()),
}

/// Rust types that can be mapped to interfaceable lushui types.
Expand Down Expand Up @@ -77,6 +84,16 @@ simple_value_correspondence! {
i64 => Int64,
}

impl IntoValue for ! {
fn into_type() -> Type {
Type::Void
}

fn into_value(self) -> Value {
self
}
}

impl IntoValue for () {
fn into_type() -> Type {
Type::Unit
Expand Down
6 changes: 6 additions & 0 deletions compiler/hir/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ pub enum Function {
Concat,
AddNat32,
Print,
Panic,
}

impl fmt::Display for Function {
Expand Down Expand Up @@ -162,11 +163,16 @@ pub fn functions() -> HashMap<Function, FunctionValue> {
arguments: vec![Value::Text(message)],
}),
);
intrinsics.insert(
Panic,
pure!(|_type: Opaque, message: Text| Value::Panic { message }),
);

intrinsics
}

macro pure(|$( $var:ident: $variant:ident ),*| $body:expr ) {
#[allow(unreachable_code)]
FunctionValue {
arity: count!($( $var )*),
function: |arguments| {
Expand Down
4 changes: 4 additions & 0 deletions compiler/hir/src/known.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/// A known binding.
#[derive(PartialEq, Eq, Hash, Clone, Copy)]
pub enum Binding {
/// The type `Void`.
Void,
/// The type `Unit`.
Unit,
/// The value `Unit.unit`.
Expand All @@ -23,6 +25,7 @@ impl Binding {
#[must_use]
pub fn parse(namespace: Option<&str>, binder: &str) -> Option<Self> {
Some(match (namespace, binder) {
(None, "Void") => Self::Void,
(None, "Unit") => Self::Unit,
(Some("Unit"), "unit") => Self::UnitUnit,
(None, "Bool") => Self::Bool,
Expand All @@ -37,6 +40,7 @@ impl Binding {

pub const fn path(self) -> &'static str {
match self {
Self::Void => "Void",
Self::Unit => "Unit",
Self::UnitUnit => "Unit.unit",
Self::Bool => "Bool",
Expand Down
49 changes: 32 additions & 17 deletions compiler/hir/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! The high-level intermediate representation.
#![feature(decl_macro, default_free_fn)]
#![feature(decl_macro, default_free_fn, never_type, never_type_fallback)]

use ast::Explicitness;
pub use entity::{Entity, EntityKind};
Expand Down Expand Up @@ -104,7 +104,7 @@ impl From<Use> for BareDeclaration {

pub type Expression = Item<BareExpression>;

#[derive(Clone)]
#[derive(Clone, Debug)]
pub enum BareExpression {
PiType(Box<PiType>),
Application(Box<Application<Expression>>),
Expand All @@ -118,7 +118,10 @@ pub enum BareExpression {
Substituted(Box<Substituted>),
IntrinsicApplication(Box<IntrinsicApplication>),
Projection(Box<Projection>),
// @Task rename to Effect
IO(Box<IO>),
// @Task make this an effect
Panic(Box<Panic>),
Error,
}

Expand All @@ -128,7 +131,7 @@ impl PossiblyErroneous for BareExpression {
}
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct PiType {
pub explicitness: Explicitness,
pub laziness: Option<Span>,
Expand Down Expand Up @@ -173,7 +176,7 @@ impl From<Binding> for BareExpression {
}
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Lambda {
pub parameter: Identifier,
pub parameter_type_annotation: Option<Expression>,
Expand All @@ -189,7 +192,7 @@ impl From<Lambda> for BareExpression {
}
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct CaseAnalysis {
pub scrutinee: Expression,
pub cases: Vec<Case>,
Expand All @@ -201,7 +204,7 @@ impl From<CaseAnalysis> for BareExpression {
}
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Substituted {
pub substitution: Substitution,
pub expression: Expression,
Expand All @@ -213,13 +216,13 @@ impl From<Substituted> for BareExpression {
}
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub enum Substitution {
Shift(usize),
Use(Box<Substitution>, Expression),
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct IntrinsicApplication {
pub callee: Identifier,
pub arguments: Vec<Expression>,
Expand All @@ -231,12 +234,12 @@ impl From<IntrinsicApplication> for BareExpression {
}
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Projection {
// @Task
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct IO {
pub index: usize, // @Task IOIndex
pub arguments: Vec<Expression>,
Expand All @@ -249,15 +252,27 @@ impl From<IO> for BareExpression {
}
}

#[derive(Clone)]
#[derive(Clone, Debug)]
// @Temporary
pub struct Panic {
pub message: String,
}

impl From<Panic> for BareExpression {
fn from(panic: Panic) -> Self {
Self::Panic(Box::new(panic))
}
}

#[derive(Clone, Debug)]
pub struct Case {
pub pattern: Pattern,
pub body: Expression,
}

pub type Pattern = Item<BarePattern>;

#[derive(Clone)]
#[derive(Clone, Debug)]
#[allow(clippy::box_collection)]
pub enum BarePattern {
Number(Box<Number>),
Expand Down Expand Up @@ -298,7 +313,7 @@ impl From<Binding> for BarePattern {
}
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Binder(pub Identifier);

impl From<Binder> for BarePattern {
Expand All @@ -313,17 +328,17 @@ impl From<Application<Pattern>> for BarePattern {
}
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Binding(pub Identifier);

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Application<T> {
pub callee: T,
pub explicitness: Explicitness,
pub argument: T,
}

#[derive(Clone, PartialEq, Eq)]
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Number {
Nat(Nat),
Nat32(u32),
Expand Down Expand Up @@ -376,7 +391,7 @@ impl fmt::Display for Number {
}
}

#[derive(Clone, PartialEq, Eq)]
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Text {
Text(String),
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/hir_format/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ fn write_lower_expression(
}
write!(f, ")")
}
// @Temporary, @Task just write it out as an application of `panic`
Panic(panic) => write!(f, "?(panic {:?})", panic.message),
Substituted(substituted) => {
write!(f, "?(substituted ",)?;
substituted.substitution.write(context, f)?;
Expand Down
2 changes: 1 addition & 1 deletion compiler/item/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use span::{Span, Spanning};
use std::default::default;

/// Something with a source location and attributes.
#[derive(Clone, PartialEq, Eq)]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Item<Bare, Attributes> {
pub bare: Bare,
pub span: Span,
Expand Down
16 changes: 8 additions & 8 deletions compiler/lowered_ast/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl Targets {
}
}

#[derive(Clone, Default)]
#[derive(Clone, Default, Debug)]
pub struct Attributes(pub Vec<Attribute>);

impl Attributes {
Expand Down Expand Up @@ -290,7 +290,7 @@ impl PossiblyErroneous for Attributes {

pub type Attribute = Spanned<BareAttribute>;

#[derive(Clone, PartialEq, Eq, Hash, Discriminant)]
#[derive(Clone, PartialEq, Eq, Hash, Discriminant, Debug)]
#[discriminant(name: AttributeName)]
pub enum BareAttribute {
/// Hide the constructors of a (public) data type.
Expand Down Expand Up @@ -754,7 +754,7 @@ data_queries! {
Public: Public = BareAttribute::Public(reach) => reach,
}

#[derive(Clone, PartialEq, Eq, Hash)]
#[derive(Clone, PartialEq, Eq, Hash, Debug)]

pub struct Deprecated {
pub reason: Option<Atom>,
Expand All @@ -763,18 +763,18 @@ pub struct Deprecated {
pub replacement: Option<Atom>,
}

#[derive(Clone, PartialEq, Eq, Hash)]
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct Public {
pub reach: Option<ast::Path>,
}

#[derive(Clone, PartialEq, Eq, Hash)]
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct Unstable {
pub feature: Feature,
pub reason: String,
}

#[derive(Clone, PartialEq, Eq, Hash)]
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum Lint {}

impl fmt::Display for Lint {
Expand All @@ -786,7 +786,7 @@ impl fmt::Display for Lint {
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum Version {}

#[derive(Clone, PartialEq, Eq, Hash)]
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum Condition {}

impl fmt::Display for Condition {
Expand All @@ -795,7 +795,7 @@ impl fmt::Display for Condition {
}
}

#[derive(Clone, PartialEq, Eq, Hash)]
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum Feature {}

impl fmt::Display for Feature {
Expand Down
1 change: 1 addition & 0 deletions compiler/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ impl FindBinding for hir::Expression {
IntrinsicApplication(_) => None, // @Task
Projection(_) => None, // @Task
IO(_) => None, // @Task
Panic(_) => None, // @Task
Error => None, // @Task
}
}
Expand Down
Loading