Delete old scoping code, working on any types
This commit is contained in:
86
ccodegen.cpp
86
ccodegen.cpp
@@ -3,7 +3,6 @@
|
|||||||
#define genln(...) do{gen("\n"); gen_indent(); gen(__VA_ARGS__); }while(0)
|
#define genln(...) do{gen("\n"); gen_indent(); gen(__VA_ARGS__); }while(0)
|
||||||
global S32 global_indent;
|
global S32 global_indent;
|
||||||
global S32 is_inside_struct;
|
global S32 is_inside_struct;
|
||||||
global B32 should_gen_scope_name = false;
|
|
||||||
|
|
||||||
function void gen_ast(Ast *ast);
|
function void gen_ast(Ast *ast);
|
||||||
function bool gen_expr(Ast_Expr *ast, Ast_Type *type_of_var = 0);
|
function bool gen_expr(Ast_Expr *ast, Ast_Type *type_of_var = 0);
|
||||||
@@ -28,7 +27,6 @@ gen_line(Ast *node){
|
|||||||
function String
|
function String
|
||||||
string_scope_name(Allocator *a, Ast_Scope *scope){
|
string_scope_name(Allocator *a, Ast_Scope *scope){
|
||||||
String string = {};
|
String string = {};
|
||||||
if(!should_gen_scope_name) return string;
|
|
||||||
if(scope->parent_scope) string = string_scope_name(a, scope->parent_scope);
|
if(scope->parent_scope) string = string_scope_name(a, scope->parent_scope);
|
||||||
if(scope->name.str) string = string_fmt(a, "%Q%Q_", string, scope->name);
|
if(scope->name.str) string = string_fmt(a, "%Q%Q_", string, scope->name);
|
||||||
return string;
|
return string;
|
||||||
@@ -44,10 +42,7 @@ gen_scope_name(Ast_Scope *scope){
|
|||||||
function String
|
function String
|
||||||
unique_name(Allocator *allocator, Ast *ast){
|
unique_name(Allocator *allocator, Ast *ast){
|
||||||
Scratch scratch;
|
Scratch scratch;
|
||||||
B32 temp = should_gen_scope_name;
|
|
||||||
should_gen_scope_name = true;
|
|
||||||
String result = string_scope_name(scratch, ast->parent_scope);
|
String result = string_scope_name(scratch, ast->parent_scope);
|
||||||
should_gen_scope_name = temp;
|
|
||||||
assert(result.len);
|
assert(result.len);
|
||||||
result = string_fmt(allocator, "%Q%d", result, ast->pos->line);
|
result = string_fmt(allocator, "%Q%d", result, ast->pos->line);
|
||||||
return result;
|
return result;
|
||||||
@@ -63,20 +58,20 @@ unique_name(Allocator *allocator, Ast *ast){
|
|||||||
// 4 Add type name on the left side
|
// 4 Add type name on the left side
|
||||||
|
|
||||||
function String
|
function String
|
||||||
string_simple_decl_prefix(Allocator *a, Ast_Type *ast, bool scope_names){
|
string_simple_decl_prefix(Allocator *a, Ast_Type *ast){
|
||||||
switch(ast->kind){
|
switch(ast->kind){
|
||||||
case TYPE_POINTER:{
|
case TYPE_POINTER:{
|
||||||
String string = string_simple_decl_prefix(a, ast->base, scope_names);
|
String string = string_simple_decl_prefix(a, ast->base);
|
||||||
string = string_fmt(a, "%Q*", string);
|
string = string_fmt(a, "%Q*", string);
|
||||||
return string;
|
return string;
|
||||||
}break;
|
}break;
|
||||||
case TYPE_LAMBDA: return {}; break;
|
case TYPE_LAMBDA: return {}; break;
|
||||||
case TYPE_ENUM:
|
case TYPE_ENUM:
|
||||||
case TYPE_ARRAY: {
|
case TYPE_ARRAY: {
|
||||||
return string_simple_decl_prefix(a, ast->base, scope_names);
|
return string_simple_decl_prefix(a, ast->base);
|
||||||
}break;
|
}break;
|
||||||
case TYPE_SLICE:{
|
case TYPE_SLICE:{
|
||||||
String string = string_simple_decl_prefix(a, ast->base, true);
|
String string = string_simple_decl_prefix(a, ast->base);
|
||||||
string = string_fmt(a, "Slice%llu ", ast->type_id);
|
string = string_fmt(a, "Slice%llu ", ast->type_id);
|
||||||
return string;
|
return string;
|
||||||
}break;
|
}break;
|
||||||
@@ -89,7 +84,6 @@ string_simple_decl_prefix(Allocator *a, Ast_Type *ast, bool scope_names){
|
|||||||
auto name = constant->name;
|
auto name = constant->name;
|
||||||
String string = string_fmt(a, "%Q ", name);
|
String string = string_fmt(a, "%Q ", name);
|
||||||
String sc = {};
|
String sc = {};
|
||||||
if(scope_names) sc = string_scope_name(a, constant->parent_scope);
|
|
||||||
return string_fmt(a, "%Q%Q", sc, string);
|
return string_fmt(a, "%Q%Q", sc, string);
|
||||||
}break;
|
}break;
|
||||||
default: {
|
default: {
|
||||||
@@ -101,13 +95,13 @@ string_simple_decl_prefix(Allocator *a, Ast_Type *ast, bool scope_names){
|
|||||||
}
|
}
|
||||||
|
|
||||||
function String
|
function String
|
||||||
string_simple_decl_postfix(Allocator *a, Ast_Type *ast, bool scope_names){
|
string_simple_decl_postfix(Allocator *a, Ast_Type *ast){
|
||||||
switch(ast->kind){
|
switch(ast->kind){
|
||||||
case TYPE_POINTER:
|
case TYPE_POINTER:
|
||||||
return string_simple_decl_postfix(a, ast->base, scope_names);
|
return string_simple_decl_postfix(a, ast->base);
|
||||||
break;
|
break;
|
||||||
case TYPE_ARRAY:{
|
case TYPE_ARRAY:{
|
||||||
String result = string_simple_decl_postfix(a, ast->arr.base, scope_names);
|
String result = string_simple_decl_postfix(a, ast->arr.base);
|
||||||
String string = string_fmt(a, "[%d]%Q", ast->arr.size, result);
|
String string = string_fmt(a, "[%d]%Q", ast->arr.size, result);
|
||||||
return string;
|
return string;
|
||||||
}break;
|
}break;
|
||||||
@@ -120,12 +114,12 @@ string_simple_decl_postfix(Allocator *a, Ast_Type *ast, bool scope_names){
|
|||||||
}
|
}
|
||||||
|
|
||||||
function String
|
function String
|
||||||
string_simple_decl(Allocator *a, Ast_Type *ast, Intern_String name = {}, Ast_Scope *scope = 0, bool scope_names = true){
|
string_simple_decl(Allocator *a, Ast_Type *ast, Intern_String name = {}){
|
||||||
if(ast->kind == TYPE_LAMBDA) {
|
if(ast->kind == TYPE_LAMBDA) {
|
||||||
String prefix = string_simple_decl_prefix(a, ast->func.ret, scope_names);
|
String prefix = string_simple_decl_prefix(a, ast->func.ret);
|
||||||
String string = string_fmt(a, "%Q(*%Q)(", prefix, name);
|
String string = string_fmt(a, "%Q(*%Q)(", prefix, name);
|
||||||
For(ast->func.args){
|
For(ast->func.args){
|
||||||
String prefix_arg = string_simple_decl_prefix(a, it, scope_names);
|
String prefix_arg = string_simple_decl_prefix(a, it);
|
||||||
string = string_fmt(a, "%Q%Q", string, prefix_arg);
|
string = string_fmt(a, "%Q%Q", string, prefix_arg);
|
||||||
if(&it != ast->func.args.end() - 1)
|
if(&it != ast->func.args.end() - 1)
|
||||||
string = string_fmt(a, "%Q, ", string);
|
string = string_fmt(a, "%Q, ", string);
|
||||||
@@ -134,29 +128,27 @@ string_simple_decl(Allocator *a, Ast_Type *ast, Intern_String name = {}, Ast_Sco
|
|||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
String string = string_simple_decl_prefix(a, ast, scope_names);
|
String string = string_simple_decl_prefix(a, ast);
|
||||||
if(name.len) {
|
if(name.len) {
|
||||||
if(scope && scope_names)
|
|
||||||
string = string_fmt(a, "%Q%Q", string, string_scope_name(a, scope));
|
|
||||||
string = string_fmt(a, "%Q%Q", string, name);
|
string = string_fmt(a, "%Q%Q", string, name);
|
||||||
}
|
}
|
||||||
String postfix = string_simple_decl_postfix(a, ast, scope_names);
|
String postfix = string_simple_decl_postfix(a, ast);
|
||||||
string = string_fmt(a, "%Q%Q", string, postfix);
|
string = string_fmt(a, "%Q%Q", string, postfix);
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function void
|
function void
|
||||||
gen_simple_decl(Ast_Type *ast, Intern_String name = {}, Ast_Scope *scope = 0, bool scope_names = true){
|
gen_simple_decl(Ast_Type *ast, Intern_String name = {}){
|
||||||
Scratch scratch;
|
Scratch scratch;
|
||||||
String string = string_simple_decl(scratch, ast, name, scope, scope_names);
|
String string = string_simple_decl(scratch, ast, name);
|
||||||
gen("%.*s", (int)string.len, string.str);
|
gen("%.*s", (int)string.len, string.str);
|
||||||
}
|
}
|
||||||
|
|
||||||
function String
|
function String
|
||||||
gen_string_simple_decl(Allocator *a, Ast_Type *ast, String name, Ast_Scope *scope, bool scope_names){
|
gen_string_simple_decl(Allocator *a, Ast_Type *ast, String name){
|
||||||
Scratch scratch;
|
Scratch scratch;
|
||||||
String string = string_simple_decl(scratch, ast, pctx->intern(name), scope, scope_names);
|
String string = string_simple_decl(scratch, ast, pctx->intern(name));
|
||||||
String result = string_copy(a, string);
|
String result = string_copy(a, string);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -213,7 +205,7 @@ enum {
|
|||||||
function void
|
function void
|
||||||
gen_var(Ast_Decl *decl, B32 emit_value, B32 scope_names){
|
gen_var(Ast_Decl *decl, B32 emit_value, B32 scope_names){
|
||||||
if(is_flag_set(decl->flags, AST_FOREIGN)) gen("extern ");
|
if(is_flag_set(decl->flags, AST_FOREIGN)) gen("extern ");
|
||||||
gen_simple_decl(decl->type, decl->name, decl->parent_scope, scope_names);
|
gen_simple_decl(decl->type, decl->name);
|
||||||
if(is_flag_set(decl->flags, AST_FOREIGN)) return;
|
if(is_flag_set(decl->flags, AST_FOREIGN)) return;
|
||||||
|
|
||||||
if(emit_value == DONT_EMIT_VALUE){
|
if(emit_value == DONT_EMIT_VALUE){
|
||||||
@@ -238,7 +230,7 @@ gen_lambda(Intern_String name, Ast_Lambda *lambda, B32 generate_block = true){
|
|||||||
if(name == pctx->intern("main"_s) || name == pctx->intern("WinMain"_s)){
|
if(name == pctx->intern("main"_s) || name == pctx->intern("WinMain"_s)){
|
||||||
is_foreign = true;
|
is_foreign = true;
|
||||||
}
|
}
|
||||||
gen_simple_decl(lambda->resolved_type->func.ret, name, lambda->parent_scope, !is_foreign);
|
gen_simple_decl(lambda->resolved_type->func.ret, name);
|
||||||
gen("(");
|
gen("(");
|
||||||
For(lambda->args){
|
For(lambda->args){
|
||||||
gen_var(it, DONT_EMIT_VALUE, true);
|
gen_var(it, DONT_EMIT_VALUE, true);
|
||||||
@@ -259,26 +251,30 @@ gen_expr(Ast_Expr *ast, Ast_Type *type_of_var){
|
|||||||
CASE(IDENT, Atom){
|
CASE(IDENT, Atom){
|
||||||
if(node->resolved_decl->kind == AST_MODULE_NAMESPACE || node->resolved_decl->kind == AST_FILE_NAMESPACE)
|
if(node->resolved_decl->kind == AST_MODULE_NAMESPACE || node->resolved_decl->kind == AST_FILE_NAMESPACE)
|
||||||
return false;
|
return false;
|
||||||
B32 print_scope = true;
|
|
||||||
if(node->resolved_decl->lambda){
|
if(type_of_var){
|
||||||
if(is_flag_set(node->resolved_decl->lambda->flags, AST_FOREIGN))
|
if(is_slice(type_of_var) && is_array(node->resolved_decl->type)){
|
||||||
print_scope = false;
|
|
||||||
}
|
|
||||||
if(type_of_var && is_slice(type_of_var) && is_array(node->resolved_decl->type)){
|
|
||||||
gen("{%d, ", (int)node->resolved_decl->type->arr.size);
|
gen("{%d, ", (int)node->resolved_decl->type->arr.size);
|
||||||
if(print_scope) gen_scope_name(node->resolved_decl->parent_scope);
|
|
||||||
gen("%Q}", node->intern_val);
|
gen("%Q}", node->intern_val);
|
||||||
} else {
|
}
|
||||||
if(print_scope) gen_scope_name(node->resolved_decl->parent_scope);
|
else if(!is_any(ast->resolved_type) && is_any(type_of_var)){
|
||||||
|
gen("(Any){&");
|
||||||
|
gen_expr(ast);
|
||||||
|
gen(", %d}", ast->resolved_type->type_id);
|
||||||
|
}
|
||||||
|
else goto otherwise;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
otherwise:
|
||||||
gen("%Q", node->intern_val);
|
gen("%Q", node->intern_val);
|
||||||
}
|
}
|
||||||
|
|
||||||
BREAK();
|
BREAK();
|
||||||
}
|
}
|
||||||
|
|
||||||
CASE(VALUE, Atom){
|
CASE(VALUE, Atom){
|
||||||
B32 written = gen_value(node->value);
|
B32 written = gen_value(node->value);
|
||||||
if(!written) {
|
if(!written) {
|
||||||
gen_scope_name(node->parent_scope);
|
|
||||||
gen("%Q", node->value.intern_val);
|
gen("%Q", node->value.intern_val);
|
||||||
}
|
}
|
||||||
BREAK();
|
BREAK();
|
||||||
@@ -511,7 +507,6 @@ gen_ast(Ast *ast){
|
|||||||
|
|
||||||
CASE(STRUCT, Decl){
|
CASE(STRUCT, Decl){
|
||||||
gen("struct ");
|
gen("struct ");
|
||||||
gen_scope_name(node->parent_scope);
|
|
||||||
gen("%Q{", node->name);
|
gen("%Q{", node->name);
|
||||||
global_indent++;
|
global_indent++;
|
||||||
is_inside_struct++;
|
is_inside_struct++;
|
||||||
@@ -783,7 +778,6 @@ function String
|
|||||||
get_compilation_result(){
|
get_compilation_result(){
|
||||||
generating_time_begin = os_time();
|
generating_time_begin = os_time();
|
||||||
|
|
||||||
#if 1
|
|
||||||
gen(R"==(
|
gen(R"==(
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
@@ -808,23 +802,22 @@ typedef S32 Bool;
|
|||||||
typedef S64 Type;
|
typedef S64 Type;
|
||||||
#define true 1
|
#define true 1
|
||||||
#define false 0
|
#define false 0
|
||||||
|
#define assert(x) do{if(!(x))__debugbreak();}while(0)
|
||||||
|
#define assert_msg(x,...) assert(x)
|
||||||
typedef struct String{
|
typedef struct String{
|
||||||
U8 *str;
|
U8 *str;
|
||||||
S64 len;
|
S64 len;
|
||||||
}String;
|
}String;
|
||||||
#define assert(x) do{if(!(x))__debugbreak();}while(0)
|
)==");
|
||||||
#define assert_msg(x,...) assert(x)
|
|
||||||
|
|
||||||
)==");
|
// Generate struct forward decls
|
||||||
//*(volatile int *)0 = 0;
|
|
||||||
#endif
|
|
||||||
For(pctx->ordered_decls){
|
For(pctx->ordered_decls){
|
||||||
if(it->kind == AST_STRUCT){
|
if(it->kind == AST_STRUCT){
|
||||||
genln("typedef struct %Q %Q;", it->name, it->name);
|
genln("typedef struct %Q %Q;", it->name, it->name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generate slice and tuple types
|
||||||
For(pctx->all_types){
|
For(pctx->all_types){
|
||||||
Scratch scratch;
|
Scratch scratch;
|
||||||
Ast_Type *type = it;
|
Ast_Type *type = it;
|
||||||
@@ -855,7 +848,7 @@ typedef struct String{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generate lambda forward decls
|
||||||
For(pctx->ordered_decls){
|
For(pctx->ordered_decls){
|
||||||
if(it->kind == AST_LAMBDA){
|
if(it->kind == AST_LAMBDA){
|
||||||
genln("");
|
genln("");
|
||||||
@@ -863,12 +856,14 @@ typedef struct String{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generate language.kl
|
||||||
for(S32 i = 0; i < pctx->base_language_ordered_decl_len; i++){
|
for(S32 i = 0; i < pctx->base_language_ordered_decl_len; i++){
|
||||||
Ast_Decl *it = pctx->ordered_decls[i];
|
Ast_Decl *it = pctx->ordered_decls[i];
|
||||||
genln("");
|
genln("");
|
||||||
gen_ast(it);
|
gen_ast(it);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generate type info
|
||||||
genln("S64 type_infos_len = %d;", pctx->all_types.len);
|
genln("S64 type_infos_len = %d;", pctx->all_types.len);
|
||||||
genln("Type_Info *type_infos = (Type_Info[]){");
|
genln("Type_Info *type_infos = (Type_Info[]){");
|
||||||
global_indent++;
|
global_indent++;
|
||||||
@@ -910,6 +905,7 @@ typedef struct String{
|
|||||||
global_indent--;
|
global_indent--;
|
||||||
gen("};");
|
gen("};");
|
||||||
|
|
||||||
|
// Generate actual code
|
||||||
for(S32 i = pctx->base_language_ordered_decl_len; i < pctx->ordered_decls.len; i++){
|
for(S32 i = pctx->base_language_ordered_decl_len; i < pctx->ordered_decls.len; i++){
|
||||||
Ast_Decl *it = pctx->ordered_decls[i];
|
Ast_Decl *it = pctx->ordered_decls[i];
|
||||||
genln("");
|
genln("");
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
|
|
||||||
|
|
||||||
main :: (argc: int, argv: **char): int
|
main :: (argc: int, argv: **char): int
|
||||||
|
test_any()
|
||||||
test_type()
|
test_type()
|
||||||
|
|
||||||
|
|
||||||
test_any :: ()
|
test_any :: ()
|
||||||
some_int_value := 10
|
some_int_value := 10
|
||||||
thing: Any = some_int_value
|
thing: Any = some_int_value
|
||||||
other_any: Any = thing
|
other_any: Any = thing
|
||||||
imp_any := thing
|
imp_any := thing
|
||||||
|
assert(thing.type != Any)
|
||||||
|
|
||||||
Some_Struct :: struct
|
Some_Struct :: struct
|
||||||
thing: int
|
thing: int
|
||||||
|
|||||||
@@ -362,9 +362,9 @@ make_sure_value_is_compatible_with_type(Token *pos, Operand *expr, Ast_Type *typ
|
|||||||
assert(type);
|
assert(type);
|
||||||
expr->type = type;
|
expr->type = type;
|
||||||
}
|
}
|
||||||
// else if(is_any(type)){
|
else if(is_any(type)){
|
||||||
// expr->type = type;
|
expr->type = type;
|
||||||
// }
|
}
|
||||||
else if(is_void_pointer(type) && is_pointer(expr->type)){
|
else if(is_void_pointer(type) && is_pointer(expr->type)){
|
||||||
expr->type = type;
|
expr->type = type;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user