summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2020-01-06 17:17:03 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2020-01-06 17:17:03 -0500
commitb22a2b562bdb2f3ca4a6ae304f9231d48f441f9d (patch)
treeccd1dea2437aac0b8a9a70d29885c348bc276fb2
parent3d226be3dd8f450333a6f381a9e4ff8011b94f60 (diff)
started importing
-rw-r--r--cgen.c30
-rw-r--r--decls_cgen.c2
-rw-r--r--eval.c3
-rw-r--r--main.c2
-rw-r--r--test.toc16
-rw-r--r--types.c26
-rw-r--r--types.h1
7 files changed, 42 insertions, 38 deletions
diff --git a/cgen.c b/cgen.c
index b94cdfc..36ee576 100644
--- a/cgen.c
+++ b/cgen.c
@@ -423,36 +423,36 @@ static inline void cgen_fn_instance_number(CGenerator *g, U64 instance) {
cgen_write(g, U64_FMT"_", instance);
}
-/* does this type have a type type in it? (e.g. [5]Type, &&Type) */
-static bool type_contains_type(Type *t) {
+/* does this type have a Type or a Package in it? (e.g. [5]Type, &&Package) */
+static bool type_contains_compileonly_type(Type *t) {
assert(t->flags & TYPE_IS_RESOLVED);
switch (t->kind) {
case TYPE_BUILTIN:
case TYPE_VOID:
case TYPE_UNKNOWN:
- case TYPE_PKG:
return false;
+ case TYPE_PKG:
case TYPE_TYPE:
return true;
case TYPE_PTR:
- return type_contains_type(t->ptr);
+ return type_contains_compileonly_type(t->ptr);
case TYPE_SLICE:
- return type_contains_type(t->slice);
+ return type_contains_compileonly_type(t->slice);
case TYPE_ARR:
- return type_contains_type(t->arr.of);
+ return type_contains_compileonly_type(t->arr.of);
case TYPE_FN:
arr_foreach(t->fn.types, Type, sub)
- if (type_contains_type(sub))
+ if (type_contains_compileonly_type(sub))
return true;
return false;
case TYPE_TUPLE:
arr_foreach(t->tuple, Type, sub)
- if (type_contains_type(sub))
+ if (type_contains_compileonly_type(sub))
return true;
return false;
case TYPE_STRUCT:
arr_foreach(t->struc->fields, Field, f)
- if (type_contains_type(f->type))
+ if (type_contains_compileonly_type(f->type))
return true;
return false;
case TYPE_EXPR: break;
@@ -465,11 +465,11 @@ static bool type_contains_type(Type *t) {
static bool cgen_should_gen_fn(FnExpr *f) {
if (f->ret_decls) {
arr_foreach(f->ret_decls, Declaration, decl)
- if (type_contains_type(&decl->type))
+ if (type_contains_compileonly_type(&decl->type))
return false;
return true;
} else {
- return !type_contains_type(&f->ret_type);
+ return !type_contains_compileonly_type(&f->ret_type);
}
}
@@ -1762,13 +1762,7 @@ static bool cgen_decl(CGenerator *g, Declaration *d) {
Identifier i = d->idents[idx];
Type *type = decl_type_at_index(d, idx);
Value *val = decl_val_at_index(d, idx);
- if (type->kind == TYPE_TYPE) {
- /*
- confusingly,
- struct declarations are handled by typedefs_cgen,
- and struct definitions are handled by decls_cgen.
- we don't need to do anything here.
- */
+ if (type_contains_compileonly_type(type)) {
continue;
}
if (g->block == NULL && g->fn == NULL && !i->export_name)
diff --git a/decls_cgen.c b/decls_cgen.c
index 85c574c..b6e7e8b 100644
--- a/decls_cgen.c
+++ b/decls_cgen.c
@@ -152,7 +152,7 @@ static bool cgen_decls_decl(CGenerator *g, Declaration *d) {
for (int i = 0, n_idents = (int)arr_len(d->idents); i < n_idents; ++i) {
Identifier ident = d->idents[i];
Type *type = decl_type_at_index(d, i);
- if (type->kind != TYPE_TYPE) {
+ if (!type_contains_compileonly_type(type)) {
if (ident->export_name)
cgen_write(g, "extern ");
else
diff --git a/eval.c b/eval.c
index 01e8964..4961f32 100644
--- a/eval.c
+++ b/eval.c
@@ -1514,6 +1514,9 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) {
case EXPR_VAL:
*v = e->val;
break;
+ case EXPR_PKG:
+ v->pkg = e->pkg.name_ident->pkg;
+ break;
case EXPR_DSIZEOF:
case EXPR_DALIGNOF:
assert(0);
diff --git a/main.c b/main.c
index f848130..8670750 100644
--- a/main.c
+++ b/main.c
@@ -118,7 +118,7 @@ int main(int argc, char **argv) {
Evaluator ev;
Exporter exptr;
evalr_create(&ev, &tr, &main_allocr);
- typer_create(&tr, &ev, &main_allocr);
+ typer_create(&tr, &ev, &main_allocr, &idents);
tr.exptr = &exptr;
if (!block_enter(NULL, f.stmts, SCOPE_CHECK_REDECL)) /* enter global scope */
diff --git a/test.toc b/test.toc
index 566bb72..61370d1 100644
--- a/test.toc
+++ b/test.toc
@@ -6,18 +6,4 @@ putf ::= fn(x: float) {
#C("printf(\"%f\\n\", (double)x);
");
};
-foo ::= fn() <int, int> {
- x, y := 3, 5;
- x, y
-};
-
-main ::= fn() {
- a, b := foo();
- x , y::= (int, int);
- puti(a); puti(b);
- c:x = 7;
- d:x = 18;
- k : [5]struct {x: int; y: int;};
- puti(#alignof(k));
- puti(k[0].x);
-}; \ No newline at end of file
+point ::= pkg "point";
diff --git a/types.c b/types.c
index 778b9a4..dbc8b09 100644
--- a/types.c
+++ b/types.c
@@ -854,12 +854,31 @@ static bool types_expr(Typer *tr, Expression *e) {
t->kind = TYPE_BUILTIN;
t->builtin = BUILTIN_CHAR;
break;
- case EXPR_PKG:
+ case EXPR_PKG: {
t->kind = TYPE_PKG;
Expression *name_expr = e->pkg.name_expr;
if (!types_expr(tr, name_expr)) return false;
+ if (!type_is_slicechar(&name_expr->type)) {
+ char *s = type_to_str(&name_expr->type);
+ err_print(name_expr->where, "Package name is not of type []char (as it should be), but of type %s.", s);
+ free(s);
+ return false;
+ }
Value name_val;
- break;
+ if (!eval_expr(tr->evalr, name_expr, &name_val))
+ return false;
+
+ Slice name_str = name_val.slice;
+ if (name_str.n < 0) {
+ err_print(name_expr->where, "Package name has negative length (" I64_FMT ")!", name_str.n);
+ return false;
+ }
+ char *name_cstr = err_malloc((size_t)name_str.n + 1);
+ memcpy(name_cstr, name_str.data, (size_t)name_str.n);
+ name_cstr[name_str.n] = '\0';
+ Identifier name_ident = ident_insert(tr->idents, &name_cstr);
+ e->pkg.name_ident = name_ident;
+ } break;
case EXPR_EACH: {
EachExpr *ea = e->each;
*(Expression **)typer_arr_add(tr, &tr->in_expr_decls) = e;
@@ -2112,7 +2131,7 @@ static bool types_stmt(Typer *tr, Statement *s) {
return true;
}
-static void typer_create(Typer *tr, Evaluator *ev, Allocator *allocr) {
+static void typer_create(Typer *tr, Evaluator *ev, Allocator *allocr, Identifiers *idents) {
tr->block = NULL;
tr->blocks = NULL;
tr->fn = NULL;
@@ -2122,6 +2141,7 @@ static void typer_create(Typer *tr, Evaluator *ev, Allocator *allocr) {
tr->in_expr_decls = NULL;
tr->pkg_name = NULL;
tr->allocr = allocr;
+ tr->idents = idents;
*(Block **)arr_adda(&tr->blocks, allocr) = NULL;
}
diff --git a/types.h b/types.h
index de945ed..67eb4f1 100644
--- a/types.h
+++ b/types.h
@@ -758,6 +758,7 @@ typedef struct Evaluator {
typedef struct Typer {
Allocator *allocr;
Evaluator *evalr;
+ Identifiers *idents;
struct Exporter *exptr;
Expression **in_expr_decls; /* an array of expressions whose declarations (e.g. each **x := foo**) we are currently inside */
Declaration **in_decls; /* array of declarations we are currently inside */