Skip to content

Commit

Permalink
Add panic builtin function
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasWanke committed Aug 15, 2024
1 parent 0366236 commit 7ea0132
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions compiler_v4/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ pub enum BuiltinFunction {
IntCompareTo,
IntSubtract,
IntToText,
Panic,
Print,
TextConcat,
}
Expand Down Expand Up @@ -306,6 +307,11 @@ impl BuiltinFunction {
parameters: [("int".into(), Type::int())].into(),
return_type: Type::text(),
},
Self::Panic => BuiltinFunctionSignature {
name: "panic".into(),
parameters: [("message".into(), Type::text())].into(),
return_type: Type::never(),
},
Self::Print => BuiltinFunctionSignature {
name: "print".into(),
parameters: [("message".into(), Type::text())].into(),
Expand Down
8 changes: 8 additions & 0 deletions compiler_v4/src/hir_to_c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,14 @@ impl<'h> Context<'h> {
return result_pointer;",
int = parameters[0].id,
)),
BuiltinFunction::Panic => {
self.push(format!(
"\
fputs({}->value);
exit(1);",
parameters[0].id,
));
}
BuiltinFunction::Print => {
self.push(format!("puts({}->value);\n", parameters[0].id));
let nothing_id = Id::from_usize(1);
Expand Down

0 comments on commit 7ea0132

Please sign in to comment.