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

Treat responsibilities like other parameters #645

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
40 changes: 20 additions & 20 deletions compiler/backend_inkwell/candy_runtime/candy_builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <string.h>
#include "candy_runtime.h"

const candy_value_t *candy_builtin_equals(candy_value_t *left, candy_value_t *right)
const candy_value_t *candy_builtin_equals(candy_value_t *left, candy_value_t *right, candy_value_t *responsible)
{
if (left
->type != right->type)
Expand All @@ -22,25 +22,25 @@ const candy_value_t *candy_builtin_equals(candy_value_t *left, candy_value_t *ri
}
}

const candy_value_t *candy_builtin_if_else(candy_value_t *condition, candy_value_t *then, candy_value_t *otherwise)
const candy_value_t *candy_builtin_if_else(candy_value_t *condition, candy_value_t *then, candy_value_t *otherwise, candy_value_t *responsible)
{
candy_value_t *body = candy_tag_to_bool(condition) ? then : otherwise;
candy_function function = (body->value).function.function;
candy_value_t *environment = (body->value).function.environment;
return function(environment);
}

candy_value_t *candy_builtin_int_add(candy_value_t *left, candy_value_t *right)
candy_value_t *candy_builtin_int_add(candy_value_t *left, candy_value_t *right, candy_value_t *responsible)
{
return make_candy_int(left->value.integer + right->value.integer);
}

candy_value_t *candy_builtin_int_subtract(candy_value_t *left, candy_value_t *right)
candy_value_t *candy_builtin_int_subtract(candy_value_t *left, candy_value_t *right, candy_value_t *responsible)
{
return make_candy_int(left->value.integer - right->value.integer);
}

candy_value_t *candy_builtin_int_bit_length(candy_value_t *value)
candy_value_t *candy_builtin_int_bit_length(candy_value_t *value, candy_value_t *responsible)
{
int64_t int_value = value->value.integer;
int is_negative = int_value < 0;
Expand All @@ -57,22 +57,22 @@ candy_value_t *candy_builtin_int_bit_length(candy_value_t *value)
return make_candy_int(shifts + is_negative);
}

candy_value_t *candy_builtin_int_bitwise_and(candy_value_t *left, candy_value_t *right)
candy_value_t *candy_builtin_int_bitwise_and(candy_value_t *left, candy_value_t *right, candy_value_t *responsible)
{
return make_candy_int(left->value.integer & right->value.integer);
}

candy_value_t *candy_builtin_int_bitwise_or(candy_value_t *left, candy_value_t *right)
candy_value_t *candy_builtin_int_bitwise_or(candy_value_t *left, candy_value_t *right, candy_value_t *responsible)
{
return make_candy_int(left->value.integer | right->value.integer);
}

candy_value_t *candy_builtin_int_bitwise_xor(candy_value_t *left, candy_value_t *right)
candy_value_t *candy_builtin_int_bitwise_xor(candy_value_t *left, candy_value_t *right, candy_value_t *responsible)
{
return make_candy_int(left->value.integer ^ right->value.integer);
}

const candy_value_t *candy_builtin_int_compare_to(candy_value_t *left, candy_value_t *right)
const candy_value_t *candy_builtin_int_compare_to(candy_value_t *left, candy_value_t *right, candy_value_t *responsible)
{
int64_t left_value = left->value.integer;
int64_t right_value = right->value.integer;
Expand All @@ -90,7 +90,7 @@ const candy_value_t *candy_builtin_int_compare_to(candy_value_t *left, candy_val
}
}

candy_value_t *candy_builtin_list_length(const candy_value_t *list)
candy_value_t *candy_builtin_list_length(const candy_value_t *list, candy_value_t *responsible)
{
size_t index = 0;
while (list->value.list[index] != NULL)
Expand All @@ -100,55 +100,55 @@ candy_value_t *candy_builtin_list_length(const candy_value_t *list)
return make_candy_int(index);
}

const candy_value_t *candy_builtin_print(candy_value_t *value)
const candy_value_t *candy_builtin_print(candy_value_t *value, candy_value_t *responsible)
{
print_candy_value(value);
printf("\n");
return &__internal_nothing;
}

candy_value_t *candy_builtin_struct_get(candy_value_t *structure, candy_value_t *key)
candy_value_t *candy_builtin_struct_get(candy_value_t *structure, candy_value_t *key, candy_value_t *responsible)
{
size_t index = 0;
while (!candy_tag_to_bool(candy_builtin_equals(structure->value.structure.keys[index], key)))
while (!candy_tag_to_bool(candy_builtin_equals(structure->value.structure.keys[index], key, responsible)))
{
index++;
}
return structure->value.structure.values[index];
}

candy_value_t *candy_builtin_struct_get_keys(candy_value_t *structure)
candy_value_t *candy_builtin_struct_get_keys(candy_value_t *structure, candy_value_t *responsible)
{
return make_candy_list(structure->value.structure.keys);
}

const candy_value_t *candy_builtin_struct_has_key(candy_value_t *structure, candy_value_t *key)
const candy_value_t *candy_builtin_struct_has_key(candy_value_t *structure, candy_value_t *key, candy_value_t *responsible)
{
size_t index = 0;
while (structure->value.structure.keys[index] != NULL)
{
if (candy_tag_to_bool(candy_builtin_equals(structure->value.structure.keys[index], key)))
if (candy_tag_to_bool(candy_builtin_equals(structure->value.structure.keys[index], key, responsible)))
{
return &__internal_true;
}
}
return &__internal_false;
}

const candy_value_t *candy_builtin_tag_has_value(candy_value_t *tag)
const candy_value_t *candy_builtin_tag_has_value(candy_value_t *tag, candy_value_t *responsible)
{
return to_candy_bool(tag->value.tag.value != NULL);
}
candy_value_t *candy_builtin_tag_get_value(candy_value_t *tag)
candy_value_t *candy_builtin_tag_get_value(candy_value_t *tag, candy_value_t *responsible)
{
return tag->value.tag.value;
}
candy_value_t *candy_builtin_tag_without_value(candy_value_t *tag)
candy_value_t *candy_builtin_tag_without_value(candy_value_t *tag, candy_value_t *responsible)
{
return make_candy_tag(tag->value.tag.text, NULL);
}

const candy_value_t *candy_builtin_type_of(candy_value_t *value)
const candy_value_t *candy_builtin_type_of(candy_value_t *value, candy_value_t *responsible)
{
switch (value->type)
{
Expand Down
36 changes: 18 additions & 18 deletions compiler/backend_inkwell/candy_runtime/candy_builtin.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include "candy_runtime.h"

const candy_value_t *candy_builtin_equals(candy_value_t *left, candy_value_t *right);
const candy_value_t *candy_builtin_if_else(candy_value_t *condition, candy_value_t *then, candy_value_t *otherwise);
candy_value_t *candy_builtin_int_add(candy_value_t *left, candy_value_t *right);
candy_value_t *candy_builtin_int_subtract(candy_value_t *left, candy_value_t *right);
candy_value_t *candy_builtin_int_bit_length(candy_value_t *value);
candy_value_t *candy_builtin_int_bitwise_and(candy_value_t *left, candy_value_t *right);
candy_value_t *candy_builtin_int_bitwise_or(candy_value_t *left, candy_value_t *right);
candy_value_t *candy_builtin_int_bitwise_xor(candy_value_t *left, candy_value_t *right);
const candy_value_t *candy_builtin_int_compare_to(candy_value_t *left, candy_value_t *right);
candy_value_t *candy_builtin_list_length(const candy_value_t *list);
const candy_value_t *candy_builtin_print(candy_value_t *value);
candy_value_t *candy_builtin_struct_get(candy_value_t *structure, candy_value_t *key);
candy_value_t *candy_builtin_struct_get_keys(candy_value_t *structure);
const candy_value_t *candy_builtin_tag_has_value(candy_value_t *tag);
candy_value_t *candy_builtin_tag_get_value(candy_value_t *tag);
candy_value_t *candy_builtin_tag_without_value(candy_value_t *tag);
const candy_value_t *candy_builtin_struct_has_key(candy_value_t *structure, candy_value_t *key);
const candy_value_t *candy_builtin_type_of(candy_value_t *value);
const candy_value_t *candy_builtin_equals(candy_value_t *left, candy_value_t *right, candy_value_t *responsible);
const candy_value_t *candy_builtin_if_else(candy_value_t *condition, candy_value_t *then, candy_value_t *otherwise, candy_value_t *responsible);
candy_value_t *candy_builtin_int_add(candy_value_t *left, candy_value_t *right, candy_value_t *responsible);
candy_value_t *candy_builtin_int_subtract(candy_value_t *left, candy_value_t *right, candy_value_t *responsible);
candy_value_t *candy_builtin_int_bit_length(candy_value_t *value, candy_value_t *responsible);
candy_value_t *candy_builtin_int_bitwise_and(candy_value_t *left, candy_value_t *right, candy_value_t *responsible);
candy_value_t *candy_builtin_int_bitwise_or(candy_value_t *left, candy_value_t *right, candy_value_t *responsible);
candy_value_t *candy_builtin_int_bitwise_xor(candy_value_t *left, candy_value_t *right, candy_value_t *responsible);
const candy_value_t *candy_builtin_int_compare_to(candy_value_t *left, candy_value_t *right, candy_value_t *responsible);
candy_value_t *candy_builtin_list_length(const candy_value_t *list, candy_value_t *responsible);
const candy_value_t *candy_builtin_print(candy_value_t *value, candy_value_t *responsible);
candy_value_t *candy_builtin_struct_get(candy_value_t *structure, candy_value_t *key, candy_value_t *responsible);
candy_value_t *candy_builtin_struct_get_keys(candy_value_t *structure, candy_value_t *responsible);
const candy_value_t *candy_builtin_tag_has_value(candy_value_t *tag, candy_value_t *responsible);
candy_value_t *candy_builtin_tag_get_value(candy_value_t *tag, candy_value_t *responsible);
candy_value_t *candy_builtin_tag_without_value(candy_value_t *tag, candy_value_t *responsible);
const candy_value_t *candy_builtin_struct_has_key(candy_value_t *structure, candy_value_t *key, candy_value_t *responsible);
const candy_value_t *candy_builtin_type_of(candy_value_t *value, candy_value_t *responsible);
9 changes: 7 additions & 2 deletions compiler/backend_inkwell/candy_runtime/candy_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ void print_candy_value(const candy_value_t *value)
break;
case CANDY_TYPE_LIST:
printf("(");
candy_value_t *length = candy_builtin_list_length(value);
// TODO: The responsible parameter is not by builtin_list_length
// anyways, so we just pass anything.
candy_value_t *length = candy_builtin_list_length(value, value);
size_t list_length = length->value.integer;
free_candy_value(length);
size_t index = 0;
Expand Down Expand Up @@ -189,11 +191,14 @@ void *get_candy_function_environment(candy_value_t *function)
return function->value.function.environment;
}

void candy_panic(const candy_value_t *reason)
void candy_panic(const candy_value_t *reason, const candy_value_t* responsible)
{
printf("The program panicked for the following reason: \n");
print_candy_value(reason);
printf("\n");
printf("The code at ");
print_candy_value(responsible);
printf(" is responsible.");
exit(-1);
}

Expand Down
13 changes: 7 additions & 6 deletions compiler/backend_inkwell/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,9 +559,7 @@ impl<'ctx> CodeGen<'ctx> {
original_hirs,
parameters,
body,
responsible_parameter,
} => {
self.unrepresented_ids.insert(*responsible_parameter);
let name = original_hirs
.iter()
.sorted()
Expand Down Expand Up @@ -652,9 +650,7 @@ impl<'ctx> CodeGen<'ctx> {
candy_frontend::mir::Expression::Call {
function,
arguments,
responsible,
} => {
self.unrepresented_ids.insert(*responsible);
let mut args: Vec<_> = arguments
.iter()
.map(|arg| self.get_value_with_id(function_ctx, arg).unwrap().into())
Expand Down Expand Up @@ -730,12 +726,17 @@ impl<'ctx> CodeGen<'ctx> {
}
}
candy_frontend::mir::Expression::UseModule { .. } => unreachable!(),
candy_frontend::mir::Expression::Panic { reason, .. } => {
candy_frontend::mir::Expression::Panic {
reason,
responsible,
} => {
let panic_fn = self.module.get_function("candy_panic").unwrap();

let reason = self.get_value_with_id(function_ctx, reason).unwrap();
let responsible = self.get_value_with_id(function_ctx, responsible).unwrap();

self.builder.build_call(panic_fn, &[reason.into()], "");
self.builder
.build_call(panic_fn, &[reason.into(), responsible.into()], "");

self.builder.build_unreachable();

Expand Down
4 changes: 2 additions & 2 deletions compiler/cli/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ pub(crate) fn run(options: Options) -> ProgramResult {
let vm = Vm::for_function(
byte_code.clone(),
&mut heap,
main,
&[environment_object],
platform,
main,
&[environment_object, platform.into()],
StackTracer::default(),
);
let VmFinished { result, .. } = vm.run_forever_with_environment(&mut heap, &mut environment);
Expand Down
3 changes: 2 additions & 1 deletion compiler/frontend/src/builtin_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ impl BuiltinFunction {

#[must_use]
pub const fn num_parameters(&self) -> usize {
match self {
// Responsibility parameter.
1 + match self {
Self::Equals => 2,
Self::FunctionRun => 1,
Self::GetArgumentCount => 1,
Expand Down
Loading