Skip to content

Commit

Permalink
misc updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaquraish committed Dec 15, 2023
1 parent abfe35e commit 8de8b66
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
19 changes: 15 additions & 4 deletions bootstrap/stage0.c
Original file line number Diff line number Diff line change
Expand Up @@ -5826,7 +5826,7 @@ ast_nodes_Function *parser_Parser_parse_function(parser_Parser *this) {
parser_Parser_consume(this, tokens_TokenType_CloseParen);
if (parser_Parser_consume_if(this, tokens_TokenType_Colon)) {
func->return_type=parser_Parser_parse_type(this);
} else if (str_eq(name, "main")) {
} else if (str_eq(func->sym->display, "main")) {
func->return_type=types_Type_new_unresolved_base(types_BaseType_I32, name_span);
} else {
func->return_type=types_Type_new_unresolved_base(types_BaseType_Void, name_span);
Expand Down Expand Up @@ -9606,7 +9606,7 @@ void passes_typechecker_TypeChecker_check_function(passes_typechecker_TypeChecke

passes_generic_pass_GenericPass_push_scope(this->o, new_scope);
passes_typechecker_TypeChecker_check_statement(this, func->body);
if (((!func->body->returns && (func->return_type->base != types_BaseType_Void)) && !str_eq(func->sym->name, "main"))) {
if (((!func->body->returns && (func->return_type->base != types_BaseType_Void)) && !str_eq(func->sym->display, "main"))) {
passes_typechecker_TypeChecker_error(this, errors_Error_new(func->sym->span, "Function does not always return"));
}
passes_generic_pass_GenericPass_pop_scope(this->o);
Expand Down Expand Up @@ -11716,7 +11716,7 @@ bool types_Type_eq(types_Type *this, types_Type *other, bool strict) {
if ((((u32)this->base) < ((u32)types_BaseType_NUM_BASE_TYPES))) {
return true;
}
ae_assert(false, "compiler/types.oc:204:20: Assertion failed: `false`", format_string("Unhandled case in Type::eq(), base = %s", types_BaseType_dbg(this->base))); exit(1); } break;
ae_assert(false, "compiler/types.oc:205:20: Assertion failed: `false`", format_string("Unhandled case in Type::eq(), base = %s", types_BaseType_dbg(this->base))); exit(1); } break;
}
}

Expand Down Expand Up @@ -11747,7 +11747,18 @@ char *types_Type_str(types_Type *this) {
__yield_0 = format_string("&%s", types_Type_str(this->u.ptr));
} break;
case types_BaseType_Function: {
__yield_0 = "<function>";
std_buffer_Buffer buf = std_buffer_Buffer_make(((u32)16));
std_buffer_Buffer_puts(&buf, "fn(");
for (u32 i = ((u32)0); (i < this->u.func.params->size); i+=((u32)1)) {
ast_nodes_Variable *param = std_vector_Vector__10_at(this->u.func.params, i);
std_buffer_Buffer_puts(&buf, types_Type_str(param->type));
if ((i < (this->u.func.params->size - ((u32)1)))) {
std_buffer_Buffer_puts(&buf, ", ");
}
}
std_buffer_Buffer_puts(&buf, "): ");
std_buffer_Buffer_puts(&buf, types_Type_str(this->u.func.return_type));
return std_buffer_Buffer_str(&buf);
} break;
case types_BaseType_Structure: {
__yield_0 = this->u.struc->sym->display;
Expand Down
2 changes: 1 addition & 1 deletion compiler/parser.oc
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,7 @@ def Parser::parse_function(&this): &Function {

if .consume_if(TokenType::Colon) {
func.return_type = .parse_type()
} else if name.eq("main") {
} else if func.sym.display.eq("main") {
func.return_type = Type::new_unresolved_base(BaseType::I32, name_span)
} else {
func.return_type = Type::new_unresolved_base(BaseType::Void, name_span)
Expand Down
2 changes: 1 addition & 1 deletion compiler/passes/typechecker.oc
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,7 @@ def TypeChecker::check_function(&this, func: &Function) {

.o.push_scope(new_scope)
.check_statement(func.body)
if not func.body.returns and func.return_type.base != BaseType::Void and not func.sym.name.eq("main") {
if not func.body.returns and func.return_type.base != BaseType::Void and not func.sym.display.eq("main") {
.error(Error::new(func.sym.span, "Function does not always return"))
}
.o.pop_scope()
Expand Down
16 changes: 15 additions & 1 deletion compiler/types.oc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import @ast::nodes::{ AST, ASTType, Structure, Enum, Variable, Function }
import @ast::scopes::{ Symbol, TemplateInstance }
import @ast::program::Namespace
import std::buffer::Buffer
import std::vector::Vector
import std::map::Map
import std::span::Span
Expand Down Expand Up @@ -224,7 +225,20 @@ def Type::decay_array(&this): &Type {

def Type::str(&this): str => match .base {
Pointer => `&{.u.ptr.str()}`
Function => "<function>"
Function => {
let buf = Buffer::make()
buf.puts("fn(")
for let i = 0; i < .u.func.params.size; i += 1 {
let param = .u.func.params.at(i)
buf.puts(param.type.str())
if i < .u.func.params.size - 1 {
buf.puts(", ")
}
}
buf.puts("): ")
buf.puts(.u.func.return_type.str())
return buf.str()
}
Structure => .u.struc.sym.display
Enum => .u.enum_.sym.display
Alias => .name
Expand Down

0 comments on commit 8de8b66

Please sign in to comment.