Initial working version of types as expressions
This commit is contained in:
69
new_ast.cpp
69
new_ast.cpp
@@ -123,7 +123,6 @@ struct Ast{
|
||||
};
|
||||
|
||||
struct Ast_Resolved_Type;
|
||||
struct Ast_Typespec;
|
||||
struct Ast_Expr:Ast{};
|
||||
|
||||
struct Ast_Atom: Ast_Expr{
|
||||
@@ -141,7 +140,7 @@ struct Ast_Compound_Item: Ast_Expr{
|
||||
|
||||
struct Ast_Compound: Ast_Expr{
|
||||
Ast_Resolved_Type *type;
|
||||
Ast_Typespec *typespec;
|
||||
Ast_Expr *typespec;
|
||||
Array<Ast_Compound_Item *> exprs;
|
||||
};
|
||||
|
||||
@@ -152,7 +151,7 @@ struct Ast_Unary: Ast_Expr{
|
||||
|
||||
struct Ast_Cast: Ast_Expr{
|
||||
Ast_Expr *expr;
|
||||
Ast_Typespec *typespec;
|
||||
Ast_Expr *typespec;
|
||||
};
|
||||
|
||||
struct Ast_Index: Ast_Expr{
|
||||
@@ -206,24 +205,12 @@ struct Ast_Array: Ast_Expr{
|
||||
Ast_Expr *expr;
|
||||
};
|
||||
|
||||
struct Ast_Typespec:Ast{
|
||||
union{
|
||||
Ast_Typespec *base;
|
||||
Intern_String name;
|
||||
struct{
|
||||
Ast_Typespec *base;
|
||||
Ast_Expr *expr;
|
||||
}arr;
|
||||
Ast_Lambda *lambda;
|
||||
};
|
||||
};
|
||||
|
||||
struct Ast_Named:Ast{
|
||||
Intern_String name;
|
||||
};
|
||||
|
||||
struct Ast_Var: Ast_Named{
|
||||
Ast_Typespec *typespec;
|
||||
Ast_Expr *typespec;
|
||||
Ast_Expr *expr;
|
||||
};
|
||||
|
||||
@@ -237,7 +224,6 @@ struct Ast_Package:Ast{
|
||||
Array<Ast_Named *> ordered;
|
||||
};
|
||||
|
||||
function Ast_Typespec *ast_typespec_name(Token *pos, Intern_String name);
|
||||
//-----------------------------------------------------------------------------
|
||||
// AST Constructors beginning with expressions
|
||||
//-----------------------------------------------------------------------------
|
||||
@@ -248,21 +234,21 @@ function Ast_Typespec *ast_typespec_name(Token *pos, Intern_String name);
|
||||
result->id = ++pctx->unique_ids
|
||||
|
||||
function Ast_Atom *
|
||||
ast_expr_string(Token *pos, Intern_String string){
|
||||
ast_str(Token *pos, Intern_String string){
|
||||
AST_NEW(Atom, STR, pos);
|
||||
result->intern_val = string;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Atom *
|
||||
ast_expr_identifier(Token *pos, Intern_String string){
|
||||
ast_ident(Token *pos, Intern_String string){
|
||||
AST_NEW(Atom, IDENT, pos);
|
||||
result->intern_val = string;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Atom *
|
||||
ast_expr_integer(Token *pos, S64 integer){
|
||||
ast_int(Token *pos, S64 integer){
|
||||
AST_NEW(Atom, INT, pos);
|
||||
result->int_val = integer;
|
||||
return result;
|
||||
@@ -278,7 +264,7 @@ ast_expr_binary(Ast_Expr *left, Ast_Expr *right, Token *op){
|
||||
}
|
||||
|
||||
function Ast_Compound *
|
||||
ast_expr_compound(Token *pos, Ast_Typespec *typespec, Array<Ast_Compound_Item *> exprs){
|
||||
ast_expr_compound(Token *pos, Ast_Expr *typespec, Array<Ast_Compound_Item *> exprs){
|
||||
AST_NEW(Compound, COMPOUND, pos);
|
||||
result->typespec = typespec;
|
||||
result->exprs = exprs.tight_copy(pctx->perm);
|
||||
@@ -295,7 +281,7 @@ ast_expr_compound_item(Token *pos, Ast_Expr *index, Ast_Atom *name, Ast_Expr *it
|
||||
}
|
||||
|
||||
function Ast_Expr *
|
||||
ast_expr_cast(Token *pos, Ast_Expr *expr, Ast_Typespec *typespec){
|
||||
ast_expr_cast(Token *pos, Ast_Expr *expr, Ast_Expr *typespec){
|
||||
AST_NEW(Cast, CAST, pos);
|
||||
result->expr = expr;
|
||||
result->typespec = typespec;
|
||||
@@ -319,13 +305,13 @@ ast_expr_index(Token *pos, Ast_Expr *expr, Ast_Expr *index){
|
||||
}
|
||||
|
||||
function Ast_Lambda *
|
||||
ast_lambda(Token *pos, Array<Ast_Lambda_Arg *> params, Ast_Typespec *ret, Ast_Block *block){
|
||||
ast_lambda(Token *pos, Array<Ast_Lambda_Arg *> params, Ast_Expr *ret, Ast_Block *block){
|
||||
AST_NEW(Lambda, LAMBDA, pos);
|
||||
result->args = params.tight_copy(pctx->perm);
|
||||
result->ret = ret;
|
||||
result->block = block;
|
||||
if(!ret){
|
||||
result->ret = ast_typespec_name(0, intern_void);
|
||||
result->ret = ast_ident(0, intern_void);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -383,44 +369,11 @@ ast_array(Token *pos, Ast_Expr *base){
|
||||
return result;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Typespecs
|
||||
//-----------------------------------------------------------------------------
|
||||
function Ast_Typespec *
|
||||
ast_typespec_name(Token *pos, Intern_String name){
|
||||
AST_NEW(Typespec, TYPESPEC_IDENT, pos);
|
||||
result->name = name;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Typespec *
|
||||
ast_typespec_pointer(Token *pos, Ast_Typespec *base){
|
||||
AST_NEW(Typespec, TYPESPEC_POINTER, pos);
|
||||
result->base = base;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Typespec *
|
||||
ast_typespec_array(Token *pos, Ast_Typespec *base, Ast_Expr *expr){
|
||||
AST_NEW(Typespec, TYPESPEC_ARRAY, pos);
|
||||
result->arr.base = base;
|
||||
result->arr.expr = expr;
|
||||
return result;
|
||||
}
|
||||
|
||||
function Ast_Typespec *
|
||||
ast_typespec_lambda(Token *pos, Ast_Lambda *lambda){
|
||||
AST_NEW(Typespec, TYPESPEC_LAMBDA, pos);
|
||||
result->lambda = lambda;
|
||||
return result;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Declarations
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
function Ast_Var *
|
||||
ast_var(Token *pos, Ast_Typespec *typespec, Intern_String name, Ast_Expr *expr){
|
||||
ast_var(Token *pos, Ast_Expr *typespec, Intern_String name, Ast_Expr *expr){
|
||||
AST_NEW(Var, VAR, pos);
|
||||
result->expr = expr;
|
||||
result->typespec = typespec;
|
||||
|
||||
Reference in New Issue
Block a user