Skip to content

Commit

Permalink
Merge pull request #1083 from candy-lang/more-builtin-functions
Browse files Browse the repository at this point in the history
Expand builtins and standard library
  • Loading branch information
JonasWanke authored Dec 8, 2024
2 parents 2ea5ecd + 9ce1d26 commit 216e7f8
Show file tree
Hide file tree
Showing 6 changed files with 598 additions and 39 deletions.
1 change: 1 addition & 0 deletions compiler_v4/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ pub struct AstImpl {

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct AstAssignment {
pub display_span: Range<Offset>,
pub name: AstResult<AstString>,
pub type_: Option<AstResult<AstType>>,
pub equals_sign_error: Option<AstError>,
Expand Down
13 changes: 9 additions & 4 deletions compiler_v4/src/ast_to_hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -985,10 +985,15 @@ impl<'a> Context<'a> {

let id = self.id_generator.generate();
// TODO: infer type
let type_ = assignment
.type_
.as_ref()
.map_or(Type::Error, |it| self.lower_type(&[], None, it.value()));
let type_ = if let Some(type_) = assignment.type_.as_ref() {
self.lower_type(&[], None, type_.value())
} else {
self.add_error(
assignment.display_span.clone(),
"Assignment is missing a type",
);
Type::Error
};

match self.global_identifiers.entry(name.string.clone()) {
Entry::Occupied(mut entry) => {
Expand Down
150 changes: 148 additions & 2 deletions compiler_v4/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ impl ToText for (&str, &TraitDefinition) {
.sorted_by_key(|(_, it)| it.signature.name.clone()),
|builder, (id, function)| (*id, *function).build_text(builder),
);
if !definition.functions.is_empty() {
builder.push_newline();
}
builder.push("}");
}
}
Expand Down Expand Up @@ -254,6 +257,9 @@ impl ToText for Impl {
(*id, *function).build_text(builder);
},
);
if !self.functions.is_empty() {
builder.push_newline();
}
builder.push("}");
}
}
Expand Down Expand Up @@ -365,6 +371,10 @@ impl NamedType {
pub fn ordering() -> Self {
Self::new("Ordering", [])
}
#[must_use]
pub fn result(t: impl Into<Type>, e: impl Into<Type>) -> Self {
Self::new("Result", [t.into(), e.into()])
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct ParameterType {
Expand Down Expand Up @@ -783,7 +793,16 @@ pub struct SwitchCase {
#[strum(serialize_all = "camelCase")]
pub enum BuiltinFunction {
IntAdd,
IntBitwiseAnd,
IntBitwiseOr,
IntBitwiseXor,
IntCompareTo,
IntDivideTruncating,
IntMultiply,
IntParse,
IntRemainder,
IntShiftLeft,
IntShiftRight,
IntSubtract,
IntToText,
ListFilled,
Expand All @@ -800,7 +819,11 @@ pub enum BuiltinFunction {
ListReplace,
Panic,
Print,
TextCompareTo,
TextConcat,
TextGetRange,
TextIndexOf,
TextLength,
}
impl BuiltinFunction {
#[must_use]
Expand All @@ -821,6 +844,36 @@ impl BuiltinFunction {
.into(),
return_type: NamedType::int().into(),
},
Self::IntBitwiseAnd => BuiltinFunctionSignature {
name: "builtinIntBitwiseAnd".into(),
type_parameters: Box::default(),
parameters: [
("a".into(), NamedType::int().into()),
("b".into(), NamedType::int().into()),
]
.into(),
return_type: NamedType::int().into(),
},
Self::IntBitwiseOr => BuiltinFunctionSignature {
name: "builtinIntBitwiseOr".into(),
type_parameters: Box::default(),
parameters: [
("a".into(), NamedType::int().into()),
("b".into(), NamedType::int().into()),
]
.into(),
return_type: NamedType::int().into(),
},
Self::IntBitwiseXor => BuiltinFunctionSignature {
name: "builtinIntBitwiseXor".into(),
type_parameters: Box::default(),
parameters: [
("a".into(), NamedType::int().into()),
("b".into(), NamedType::int().into()),
]
.into(),
return_type: NamedType::int().into(),
},
Self::IntCompareTo => BuiltinFunctionSignature {
name: "builtinIntCompareTo".into(),
type_parameters: Box::default(),
Expand All @@ -831,12 +884,68 @@ impl BuiltinFunction {
.into(),
return_type: NamedType::ordering().into(),
},
Self::IntDivideTruncating => BuiltinFunctionSignature {
name: "builtinIntDivideTruncating".into(),
type_parameters: Box::default(),
parameters: [
("dividend".into(), NamedType::int().into()),
("divisor".into(), NamedType::int().into()),
]
.into(),
return_type: NamedType::int().into(),
},
Self::IntMultiply => BuiltinFunctionSignature {
name: "builtinIntMultiply".into(),
type_parameters: Box::default(),
parameters: [
("factorA".into(), NamedType::int().into()),
("factorB".into(), NamedType::int().into()),
]
.into(),
return_type: NamedType::int().into(),
},
Self::IntParse => BuiltinFunctionSignature {
name: "builtinIntParse".into(),
type_parameters: Box::default(),
parameters: [("text".into(), NamedType::text().into())].into(),
return_type: NamedType::result(NamedType::int(), NamedType::text()).into(),
},
Self::IntRemainder => BuiltinFunctionSignature {
name: "builtinIntRemainder".into(),
type_parameters: Box::default(),
parameters: [
("dividend".into(), NamedType::int().into()),
("divisor".into(), NamedType::int().into()),
]
.into(),
return_type: NamedType::int().into(),
},
Self::IntShiftLeft => BuiltinFunctionSignature {
name: "builtinIntShiftLeft".into(),
type_parameters: Box::default(),
parameters: [
("value".into(), NamedType::int().into()),
("amount".into(), NamedType::int().into()),
]
.into(),
return_type: NamedType::int().into(),
},
Self::IntShiftRight => BuiltinFunctionSignature {
name: "builtinIntShiftRight".into(),
type_parameters: Box::default(),
parameters: [
("value".into(), NamedType::int().into()),
("amount".into(), NamedType::int().into()),
]
.into(),
return_type: NamedType::int().into(),
},
Self::IntSubtract => BuiltinFunctionSignature {
name: "builtinIntSubtract".into(),
type_parameters: Box::default(),
parameters: [
("a".into(), NamedType::int().into()),
("b".into(), NamedType::int().into()),
("minuend".into(), NamedType::int().into()),
("subtrahend".into(), NamedType::int().into()),
]
.into(),
return_type: NamedType::int().into(),
Expand Down Expand Up @@ -991,6 +1100,16 @@ impl BuiltinFunction {
parameters: [("message".into(), NamedType::text().into())].into(),
return_type: NamedType::nothing().into(),
},
Self::TextCompareTo => BuiltinFunctionSignature {
name: "builtinTextCompareTo".into(),
type_parameters: Box::default(),
parameters: [
("a".into(), NamedType::text().into()),
("b".into(), NamedType::text().into()),
]
.into(),
return_type: NamedType::ordering().into(),
},
Self::TextConcat => BuiltinFunctionSignature {
name: "builtinTextConcat".into(),
type_parameters: Box::default(),
Expand All @@ -1001,6 +1120,33 @@ impl BuiltinFunction {
.into(),
return_type: NamedType::text().into(),
},
Self::TextGetRange => BuiltinFunctionSignature {
name: "builtinTextGetRange".into(),
type_parameters: Box::default(),
parameters: [
("text".into(), NamedType::text().into()),
("startInclusive".into(), NamedType::int().into()),
("endExclusive".into(), NamedType::int().into()),
]
.into(),
return_type: NamedType::text().into(),
},
Self::TextIndexOf => BuiltinFunctionSignature {
name: "builtinTextIndexOf".into(),
type_parameters: Box::default(),
parameters: [
("a".into(), NamedType::text().into()),
("b".into(), NamedType::text().into()),
]
.into(),
return_type: NamedType::maybe(NamedType::int()).into(),
},
Self::TextLength => BuiltinFunctionSignature {
name: "builtinTextLength".into(),
type_parameters: Box::default(),
parameters: [("text".into(), NamedType::text().into())].into(),
return_type: NamedType::int().into(),
},
}
}
}
Expand Down
Loading

0 comments on commit 216e7f8

Please sign in to comment.