From: Michael D. Lowis Date: Thu, 31 May 2018 01:17:55 +0000 (-0400) Subject: Added type node constructors X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=4f40218f11eca8a71cd844f3bbcb84b2fa2f5613;p=proto%2Fsclpl.git Added type node constructors --- diff --git a/Makefile b/Makefile index 3384587..77e0a2d 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,8 @@ OBJS = source/main.o \ source/pprint.o \ source/parser.o \ source/lexer.o \ - source/ast.o + source/ast.o \ + source/types.o .PHONY: all tests specs all: sclpl tests specs diff --git a/source/main.c b/source/main.c index c5f1563..4bc946b 100644 --- a/source/main.c +++ b/source/main.c @@ -3,6 +3,27 @@ char* ARGV0; char* Artifact = "ast"; +/* Builtin Types + *****************************************************************************/ +static void builtins(Parser* p) { +#if 0 + sym_add(&(p->syms), "void", VoidType()); + sym_add(&(p->syms), "bool", UIntType(1u)); + sym_add(&(p->syms), "byte", UIntType(8u)); + sym_add(&(p->syms), "uint", UIntType(64u)); + sym_add(&(p->syms), "u8", UIntType(8u)); + sym_add(&(p->syms), "u16", UIntType(16u)); + sym_add(&(p->syms), "u32", UIntType(32u)); + sym_add(&(p->syms), "u64", UIntType(64u)); + sym_add(&(p->syms), "int", IntType(64u)); + sym_add(&(p->syms), "i8", IntType(8u)); + sym_add(&(p->syms), "i16", IntType(16u)); + sym_add(&(p->syms), "i32", IntType(32u)); + sym_add(&(p->syms), "i64", IntType(64u)); + sym_add(&(p->syms), "string", ArrayType(sym_get(&(p->syms), "byte"))); +#endif +} + /* Driver Modes *****************************************************************************/ static int emit_tokens(void) { diff --git a/source/sclpl.h b/source/sclpl.h index 21784bf..398e246 100644 --- a/source/sclpl.h +++ b/source/sclpl.h @@ -70,7 +70,7 @@ typedef struct Type { Kind kind; union { struct Type* type; - ssize_t bits; + size_t bits; struct { struct Type* type; size_t count; @@ -78,9 +78,10 @@ typedef struct Type { } value; } Type; -Type* IntType(ssize_t nbits); -Type* UIntType(ssize_t nbits); -Type* ArrayOf(Type* type); +Type* VoidType(void); +Type* IntType(size_t nbits); +Type* UIntType(size_t nbits); +Type* ArrayOf(Type* type, size_t count); Type* RefTo(Type* type); Type* PtrTo(Type* type); diff --git a/source/types.c b/source/types.c new file mode 100644 index 0000000..41db199 --- /dev/null +++ b/source/types.c @@ -0,0 +1,39 @@ +#include + +static Type* mktype(Kind kind) { + Type* type = emalloc(sizeof(Type)); + memset(type, 0, sizeof(Type)); + type->kind = kind; + return type; +} + +Type* VoidType(void) { + return mktype(VOID); +} + +Type* IntType(size_t nbits) { + Type* type = mktype(INT); + type->value.bits = nbits; + return type; +} + +Type* UIntType(size_t nbits) { + Type* type = mktype(UINT); + type->value.bits = nbits; + return type; +} + +Type* ArrayOf(Type* elemtype, size_t count) { + Type* type = mktype(ARRAY); + type->value.array.type = elemtype; + type->value.array.count = count; + return type; +} + +Type* RefTo(Type* type) { + return NULL; +} + +Type* PtrTo(Type* type) { + return NULL; +}