Skip to content

Commit

Permalink
Merge pull request #134 from ch-systems/alex/constant-types
Browse files Browse the repository at this point in the history
Introduce constant variable value types
  • Loading branch information
sezna authored Jul 30, 2024
2 parents 3b536fc + 211c783 commit 6c2873b
Show file tree
Hide file tree
Showing 22 changed files with 1,713 additions and 514 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 23 additions & 5 deletions petr-ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl ImportStatement {
#[derive(Clone)]
pub struct TypeDeclaration {
pub name: Identifier,
pub variants: Box<[SpannedItem<TypeVariant>]>,
pub variants: Box<[SpannedItem<TypeVariantOrLiteral>]>,
pub visibility: Visibility,
}

Expand All @@ -89,6 +89,21 @@ impl TypeDeclaration {
}
}

#[derive(Clone)]
pub enum TypeVariantOrLiteral {
Variant(TypeVariant),
Literal(Literal),
}

impl TypeVariantOrLiteral {
pub fn fields(&self) -> Vec<TypeField> {
match self {
TypeVariantOrLiteral::Variant(variant) => variant.fields.iter().map(|x| x.item().clone()).collect(),
TypeVariantOrLiteral::Literal(_) => vec![],
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Visibility {
Local,
Expand Down Expand Up @@ -211,11 +226,12 @@ pub struct List {
pub elements: Box<[Commented<SpannedItem<Expression>>]>,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Literal {
Integer(i64),
Boolean(bool),
String(Rc<str>),
// TODO intern these strings and use an ID
String(String),
}

impl std::fmt::Display for Literal {
Expand All @@ -238,19 +254,21 @@ pub struct OperatorExpression {
pub op: SpannedItem<Operator>,
}

#[derive(Clone, Debug, Copy)]
#[derive(Clone, Debug)]
pub struct FunctionParameter {
pub name: Identifier,
pub ty: Ty,
}

#[derive(Clone, Copy, Debug)]
#[derive(Clone, Debug)]
pub enum Ty {
Int,
Bool,
Named(Identifier),
String,
Unit,
Literal(Literal),
Sum(Box<[Ty]>),
}

#[derive(Clone)]
Expand Down
33 changes: 30 additions & 3 deletions petr-ast/src/pretty_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ impl PrettyPrint for TypeDeclaration {
}
}

impl PrettyPrint for TypeVariantOrLiteral {
fn pretty_print(
&self,
interner: &SymbolInterner,
indentation: usize,
) -> String {
match self {
TypeVariantOrLiteral::Variant(variant) => variant.pretty_print(interner, indentation),
TypeVariantOrLiteral::Literal(literal) => literal.pretty_print(interner, indentation),
}
}
}

impl PrettyPrint for TypeVariant {
fn pretty_print(
&self,
Expand Down Expand Up @@ -127,6 +140,8 @@ impl PrettyPrint for Ty {
Ty::String => "string".to_string(),
Ty::Unit => "unit".to_string(),
Ty::Named(name) => name.pretty_print(interner, 0),
Ty::Literal(lit) => format!("lit ty {}", lit.pretty_print(interner, 0)),
Ty::Sum(tys) => tys.iter().map(|ty| ty.pretty_print(interner, 0)).collect::<Vec<_>>().join(" | "),
};
format!("'{name}")
}
Expand Down Expand Up @@ -154,9 +169,7 @@ impl PrettyPrint for Expression {
indentation: usize,
) -> String {
match self {
Expression::Literal(Literal::Integer(i)) => i.to_string(),
Expression::Literal(Literal::Boolean(b)) => b.to_string(),
Expression::Literal(Literal::String(s)) => format!("\"{s}\""),
Expression::Literal(l) => l.pretty_print(interner, indentation),
Expression::List(list) => list.pretty_print(interner, indentation),
Expression::Operator(op) => op.pretty_print(interner, indentation),
Expression::TypeConstructor(..) => "type constructor".to_string(),
Expand All @@ -169,6 +182,20 @@ impl PrettyPrint for Expression {
}
}

impl PrettyPrint for Literal {
fn pretty_print(
&self,
_: &SymbolInterner,
_: usize,
) -> String {
match self {
Literal::Integer(i) => i.to_string(),
Literal::Boolean(b) => b.to_string(),
Literal::String(s) => format!("\"{s}\""),
}
}
}

impl PrettyPrint for If {
fn pretty_print(
&self,
Expand Down
8 changes: 7 additions & 1 deletion petr-bind/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ license.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
petr-utils = { path = "../petr-utils", version = "0.1.0" }
petr-utils = { path = "../petr-utils", version = "0.1.0", optional = true }
petr-ast = { path = "../petr-ast", version = "0.1.0" }


[dev-dependencies]
expect-test = "1.5.0"
petr-parse = { path = "../petr-parse", version = "0.1.0" }
petr-stdlib = { path = "../petr-stdlib", version = "0.1.0" }


[features]
debug = ["petr-utils/debug"]
default = ["dep:petr-utils"]
Loading

0 comments on commit 6c2873b

Please sign in to comment.