Skip to content

Commit

Permalink
Support the "malloc idiom" (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli authored Mar 3, 2023
1 parent e87398e commit 5b0fa1e
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 10 deletions.
2 changes: 1 addition & 1 deletion self_hosted/main.jou
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class Compiler:
nfiles: int

def parse_all_files(self, main_path: byte*) -> void:
queue: byte** = malloc(sizeof main_path)
queue: byte** = malloc(sizeof queue[0])
queue[0] = main_path
queue_len = 1

Expand Down
6 changes: 2 additions & 4 deletions self_hosted/parser.jou
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ def parse_type(tokens: Token**) -> AstType:
++*tokens

while (*tokens)->is_operator("*"):
p: AstType*
p = malloc(sizeof *p)
p: AstType* = malloc(sizeof *p)
*p = result
result = AstType{
location = ((*tokens)++)->location, # TODO: shouldn't need all the parentheses
Expand All @@ -57,8 +56,7 @@ def parse_name_type_value(tokens: Token**, expected_what_for_name: byte*) -> Ast

if (*tokens)->is_operator("="):
++*tokens
p: AstExpression*
p = malloc(sizeof *p)
p: AstExpression* = malloc(sizeof *p)
*p = parse_expression(tokens)
result.value = p

Expand Down
3 changes: 1 addition & 2 deletions self_hosted/typecheck.jou
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,7 @@ def typecheck_expression_maybe_void(ctx: TypeContext*, expression: AstExpression
assert(False)
result = NULL # never runs, but silences compiler warning

p: ExpressionTypes*
p = malloc(sizeof *p)
p: ExpressionTypes* = malloc(sizeof *p)
*p = ExpressionTypes{
expression = expression,
type = result,
Expand Down
2 changes: 1 addition & 1 deletion src/typecheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -1160,12 +1160,12 @@ static void typecheck_statement(TypeContext *ctx, const AstStatement *stmt)
fail_with_error(stmt->location, "a variable named '%s' already exists", stmt->data.vardecl.name);

const Type *type = type_from_ast(ctx, &stmt->data.vardecl.type);
add_variable(ctx, type, stmt->data.vardecl.name);
if (stmt->data.vardecl.value) {
typecheck_expression_with_implicit_cast(
ctx, stmt->data.vardecl.value, type,
"initial value for variable of type TO cannot be of type FROM");
}
add_variable(ctx, type, stmt->data.vardecl.name);
break;

case AST_STMT_EXPRESSION_STATEMENT:
Expand Down
3 changes: 1 addition & 2 deletions tests/should_succeed/linked_list.jou
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ class Node:
putchar('\n')

def prepend(list: Node**, value: byte) -> void:
new_first: Node*
new_first = malloc(sizeof *new_first)
new_first: Node* = malloc(sizeof *new_first)
*new_first = Node{value = value, next = *list}
*list = new_first

Expand Down

0 comments on commit 5b0fa1e

Please sign in to comment.