Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 80 additions & 38 deletions ast/utils.c2
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public type Globals struct @(opaque) {
#if AstStatistics
Stats stats;
#endif
Color[15] colors;
}

// The only globals for AST are here, since they must be set explicitly for plugins!!
Expand All @@ -127,26 +128,41 @@ public QualType* builtins;
public fn Globals* getGlobals() { return globals; }

// only used by plugins
public fn void setGlobals(Globals* g) @(unused){
public fn void setGlobals(Globals* g) @(unused) {
globals = g;
attr.initialize(g.attr_name_indexes);
col_Normal = g.colors[0];
col_Error = g.colors[1];
col_Warning = g.colors[2];
col_Keyword = g.colors[3];
col_Identifier = g.colors[4];
col_Literal = g.colors[5];
col_Comment = g.colors[6];
col_Stmt = g.colors[7];
col_Decl = g.colors[8];
col_Expr = g.colors[9];
col_Attr = g.colors[10];
col_Template = g.colors[11];
col_Type = g.colors[12];
col_Value = g.colors[13];
col_Calc = g.colors[14];
}


// wordsize in bytes, must NOT be called from Plugin!
public fn void initialize(Context* c, string_pool.Pool* astPool, u32 wordsize, bool use_color) {
globals = stdlib.malloc(sizeof(Globals));
globals.pointers.init(c);
globals.string_types.init(c);
globals.wordsize = wordsize;
globals.use_color = use_color;
globals.names_pool = astPool;
globals.ast_count = 1;
globals.ast_capacity = 0;
globals.ast_list = nil;
globals.dump_buf = string_buffer.create(4096, use_color, 2);
Globals* g = stdlib.malloc(sizeof(Globals));
globals = g; // needed by stats called indirectly by BuiltinType.create
g.pointers.init(c);
g.string_types.init(c);
g.wordsize = wordsize;
g.use_color = use_color;
g.names_pool = astPool;
g.ast_count = 1;
g.ast_capacity = 0;
g.ast_list = nil;
g.dump_buf = string_buffer.create(4096, use_color, 2);
#if AstStatistics
globals.stats.reset();
g.stats.reset();
#endif

// create all Qualtypes for builtin-types, 1 extra for void-ptr
Expand All @@ -160,43 +176,64 @@ public fn void initialize(Context* c, string_pool.Pool* astPool, u32 wordsize, b
builtins[elemsof(BuiltinKind)].set(void_ptr);
void_ptr.setCanonicalType(builtins[elemsof(BuiltinKind)]);

string.memcpy(globals.builtinType_sizes, BuiltinType_default_sizes, sizeof(BuiltinType_default_sizes));
globals.builtinType_sizes[BuiltinKind.ISize] = wordsize;
globals.builtinType_sizes[BuiltinKind.USize] = wordsize;
string.memcpy(g.builtinType_sizes, BuiltinType_default_sizes, sizeof(BuiltinType_default_sizes));
g.builtinType_sizes[BuiltinKind.ISize] = wordsize;
g.builtinType_sizes[BuiltinKind.USize] = wordsize;

string.memcpy(globals.builtinType_width, BuiltinType_default_widths, sizeof(BuiltinType_default_widths));
globals.builtinType_width[BuiltinKind.ISize] = wordsize * 8;
globals.builtinType_width[BuiltinKind.USize] = wordsize * 8;
string.memcpy(g.builtinType_width, BuiltinType_default_widths, sizeof(BuiltinType_default_widths));
g.builtinType_width[BuiltinKind.ISize] = wordsize * 8;
g.builtinType_width[BuiltinKind.USize] = wordsize * 8;

for (u32 i=0; i<elemsof(BuiltinKind); i++) {
globals.builtinType_baseTypes[i] = (BuiltinKind)i;
g.builtinType_baseTypes[i] = (BuiltinKind)i;
}
if (wordsize == 4) {
globals.builtinType_baseTypes[BuiltinKind.ISize] = BuiltinKind.Int32;
globals.builtinType_baseTypes[BuiltinKind.USize] = BuiltinKind.UInt32;
g.builtinType_baseTypes[BuiltinKind.ISize] = BuiltinKind.Int32;
g.builtinType_baseTypes[BuiltinKind.USize] = BuiltinKind.UInt32;
} else {
globals.builtinType_baseTypes[BuiltinKind.ISize] = BuiltinKind.Int64;
globals.builtinType_baseTypes[BuiltinKind.USize] = BuiltinKind.UInt64;
g.builtinType_baseTypes[BuiltinKind.ISize] = BuiltinKind.Int64;
g.builtinType_baseTypes[BuiltinKind.USize] = BuiltinKind.UInt64;
}

attr.register(astPool, globals.attr_name_indexes);
attr.initialize(globals.attr_name_indexes);
attr.register(astPool, g.attr_name_indexes);

g.colors[0] = color.getConfigColor("normal", color.Normal);
g.colors[1] = color.getConfigColor("error", color.Red);
g.colors[2] = color.getConfigColor("warning", color.Yellow);
g.colors[3] = color.getConfigColor("keyword", color.Green);
g.colors[4] = color.getConfigColor("identifier", color.Cyan);
g.colors[5] = color.getConfigColor("literal", color.Cyan);
g.colors[6] = color.getConfigColor("comment", color.Cyan);
g.colors[7] = color.getConfigColor("ast.stmt", color.Bmagenta);
g.colors[8] = color.getConfigColor("ast.decl", color.Bgreen);
g.colors[9] = color.getConfigColor("ast.expr", color.Bmagenta);
g.colors[10] = color.getConfigColor("ast.attr", color.Blue);
g.colors[11] = color.getConfigColor("ast.template", color.Green);
g.colors[12] = color.getConfigColor("ast.type", color.Green);
g.colors[13] = color.getConfigColor("ast.value", color.Bcyan);
g.colors[14] = color.getConfigColor("ast.calc", color.Yellow); // all calculated values

setGlobals(g);
}

public fn void deinit(bool print_stats) {
Globals* g = globals;
if (!g) return;
#if AstStatistics
if (print_stats) globals.stats.dump();
if (print_stats) g.stats.dump();
#endif
globals.names_pool = nil;
globals.ast_count = 0;
globals.ast_capacity = 0;
stdlib.free(globals.ast_list);
globals.ast_list = nil;
globals.pointers.clear();
globals.string_types.clear();
stdlib.free(globals);
g.names_pool = nil;
g.ast_count = 0;
g.ast_capacity = 0;
stdlib.free(g.ast_list);
g.ast_list = nil;
g.pointers.clear();
g.string_types.clear();
stdlib.free(builtins);
globals.dump_buf.free();
g.dump_buf.free();
stdlib.free(g);
globals = nil;
color.freeConfigColors();
}

public fn u32 getWordSize() {
Expand Down Expand Up @@ -307,6 +344,7 @@ public fn BuiltinKind getNativeKind() {
return globals.wordsize == 8 ? BuiltinKind.UInt64 : BuiltinKind.UInt32;
}

public Color col_Normal = Color.Normal;
Color col_Stmt = Color.Bmagenta;
Color col_Decl = Color.Bgreen;
Color col_Expr = Color.Bmagenta;
Expand All @@ -315,9 +353,13 @@ Color col_Template = Color.Green;
//Color col_Cast = Color.Red;
Color col_Type = Color.Green;
Color col_Value = Color.Bcyan;
Color col_Error = Color.Red;
Color col_Calc = Color.Yellow; // all calculated value
Color col_Normal = Color.Normal;
public Color col_Error = Color.Red;
public Color col_Warning = color.Yellow;
public Color col_Keyword = color.Green;
public Color col_Identifier = color.Cyan;
public Color col_Literal = color.Cyan;
public Color col_Comment = color.Cyan;

public type AttrHandlerFn fn bool (void* arg, Decl* d, const attr.Attr* a);

31 changes: 28 additions & 3 deletions ast_utils/color.c2
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
*/

module color;
import unistd;

import stdlib local;
import string local;
import unistd local;

public type Color enum u8 {
None,
Expand All @@ -35,6 +38,9 @@ public type Color enum u8 {
Bcyan,
White,
Normal,

CustomStart,
CustomEnd = Color.CustomStart + 15,
}

public const Color Black = Black;
Expand All @@ -55,7 +61,7 @@ public const Color Bcyan = Bcyan;
public const Color White = White;
public const Color Normal = Normal;

const char*[elemsof(Color)] defaultColors = {
const char*[] standardColors = {
[Color.None] = "",
[Color.Black] = "\033[0;30m",
[Color.Red] = "\033[0;31m",
Expand All @@ -76,8 +82,27 @@ const char*[elemsof(Color)] defaultColors = {
[Color.Normal] = "\033[0m",
}

i8 use_color = -1;
const char* c2_colors;
char[elemsof(Color)][32] defaultColors;

public fn bool useColor() {
return unistd.isatty(1);
if (use_color < 0) {
use_color = isatty(1) != 0;
if (use_color) {
c2_colors = getenv("C2_COLORS");
if (c2_colors && !strcmp(c2_colors, "none")) {
use_color = 0;
c2_colors = nil;
} else {
for (u32 i = 0; i < elemsof(standardColors); i++) {
strcpy(defaultColors[i], standardColors[i]);
}
if (c2_colors) customizeStandardColors(c2_colors);
}
}
}
return use_color;
}

public fn const char* Color.str(Color col) {
Expand Down
Loading