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

Expand builtins and standard library #1083

Merged
merged 27 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8be02e2
Add text.getRange(…)
JonasWanke Nov 21, 2024
1895947
Expose text.concat(…)
JonasWanke Nov 21, 2024
dc2b313
Add text.length()
JonasWanke Nov 21, 2024
bba7bbf
impl Text: Compare
JonasWanke Nov 21, 2024
828c73b
Add text.startsWith(…), .endsWith(…)
JonasWanke Nov 28, 2024
5d5b050
Add bool functions
JonasWanke Nov 28, 2024
28bcf5f
Add trait HasLength
JonasWanke Nov 28, 2024
a4b9a30
Add int.is[Non](Positive|Negative)(), .negate(), .absolute()
JonasWanke Nov 28, 2024
8b26795
Add maybe.isSome(), .isNone()
JonasWanke Nov 28, 2024
46da39a
Add text.removePrefix(…), .removeSuffix(…)
JonasWanke Nov 28, 2024
9bf4bce
Add text.characterAt(…)
JonasWanke Nov 28, 2024
dce7389
Add text.indexOf(…), .contains(…)
JonasWanke Nov 28, 2024
b3df2b8
Remove unnecessary type annotations
JonasWanke Nov 28, 2024
bcbb469
Merge branch 'main' into text-functions
JonasWanke Nov 28, 2024
c6e5f82
Revert "Remove unnecessary type annotations"
JonasWanke Nov 28, 2024
7595096
Fix builtinTextIndexOf(…)
JonasWanke Nov 28, 2024
e9d7152
Fix HIR text formatting
JonasWanke Nov 28, 2024
77af947
Add error for missing assignment type
JonasWanke Nov 28, 2024
6fe6fdb
Remove unused code
JonasWanke Nov 28, 2024
9a56de2
impl[T] List[T]: HasLength
JonasWanke Nov 28, 2024
41e0e64
Add needs(…)
JonasWanke Nov 28, 2024
910e460
Add Result[T, E]
JonasWanke Nov 28, 2024
9d80fa7
Add Int functions
JonasWanke Nov 28, 2024
fc1f001
Fix builtinIntParse(…)
JonasWanke Nov 28, 2024
5de0be8
Merge branch 'main' into more-builtin-functions
JonasWanke Nov 28, 2024
fbaea78
Fix doc comment for int.modulo(…)
JonasWanke Dec 8, 2024
9ce1d26
Merge branch 'main' into more-builtin-functions
JonasWanke Dec 8, 2024
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
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 @@ -363,6 +369,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 @@ -723,7 +733,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 @@ -740,7 +759,11 @@ pub enum BuiltinFunction {
ListReplace,
Panic,
Print,
TextCompareTo,
TextConcat,
TextGetRange,
TextIndexOf,
TextLength,
}
impl BuiltinFunction {
#[must_use]
Expand All @@ -761,6 +784,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 @@ -771,12 +824,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 @@ -931,6 +1040,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 @@ -941,6 +1060,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