commit eaec4d6f8bd856a809d4d69b6588c6019e07a76c
parent 57e2b308acc28de482a3c23710143d81337e42da
Author: m21c <ho*******@gmail.com>
Date: Sun, 3 Oct 2021 01:20:44 +0200
implemented type for function declaration
Diffstat:
1 file changed, 24 insertions(+), 7 deletions(-)
diff --git a/compiler.c b/compiler.c
@@ -2013,6 +2013,9 @@ advance:
}
static Node *
+typecheck(Env *env, Node *expr);
+
+static Node *
declaration(Source *source, Type *ty)
{
bool selfparam = false;
@@ -2073,6 +2076,7 @@ declaration(Source *source, Type *ty)
/* function declaration */
if (getkind(source) == '(') {
+ Type *paramtype = NULL;
Env *functionenv = NULL;
Node *body = NULL;
@@ -2081,12 +2085,13 @@ declaration(Source *source, Type *ty)
if (getkind(source) != ')') {
Decl *param;
Node *paramlist;
- int savedtop;
functionenv = pushenv(source, SPARAMLIST);
functionenv->envdecl = decl;
paramlist = exprlist(source, true, NULL);
+ paramlist = typecheck(functionenv, paramlist);
+ paramtype = paramlist->type;
/* deletenode(paramlist); */
for (param = functionenv->head; param;
@@ -2098,6 +2103,9 @@ declaration(Source *source, Type *ty)
}
expect(source, ')', "expected ')'");
+ decl->type = maketype(&decl->loc, prim + TFUNCTION, paramtype);
+ decl->type->u.rtarget = ty;
+
/* function body */
if (getkind(source) != OASS) {
body = stmtlist(source, source->lastindent,
@@ -2983,9 +2991,6 @@ conv(Node *node)
}
-static Node *
-typecheck(Env *env, Node *expr);
-
static bool
arithtuplereorder(Env *env, Node *expr, int numops)
{
@@ -3732,8 +3737,16 @@ printtypetail(FILE *out, Type *type, int indent)
if (!type)
return 0;
- if (type->kind != TTUPLE &&
- type->target &&
+ if (type->kind == TFUNCTION) {
+ n += printtypetail(out, type->u.rtarget, indent);
+ n += fprintf(out, " function(");
+ if (type->target)
+ n += printtypetail(out, type->target, indent);
+ n += fprintf(out, ")");
+ return n;
+ }
+
+ if (type->kind != TTUPLE && type->target &&
type->target->kind == TTUPLE)
{
n += fprintf(out, "(");
@@ -3960,7 +3973,11 @@ printdeclaration(FILE *out, Decl *decl, int indent)
assert(decl);
n += highlight(out, HLTYPE);
- n += printtype(out, decl->type, indent);
+ if (decl->kind == DFUNCTION) {
+ n += printtype(out, decl->type->u.rtarget, indent);
+ } else {
+ n += printtype(out, decl->type, indent);
+ }
n += highlight(out,
decl->kind == DFUNCTION ? HLFUNCTIONDECL :