bydgoszczscript

simple, fast and efficient programming language
git clone git://git.kocotian.pl/bydgoszczscript.git
Log | Files | Refs | README | LICENSE

commit 4e8d4dad2c8ee542319c88949ddb069d2b714bfd
parent 3a3e4e99439e2d112e4690736f6d8020daf41f2b
Author: kocotian <kocotian@kocotian.pl>
Date:   Wed, 26 May 2021 14:19:40 +0200

Types, function basics

Diffstat:
Mcompile.c | 84+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Minclude/str.h | 2++
2 files changed, 82 insertions(+), 4 deletions(-)

diff --git a/compile.c b/compile.c @@ -24,30 +24,106 @@ #include <compile.h> #include <stddef.h> #include <stdio.h> +#include <string.h> #include <errwarn.h> #include <str.h> -static Token *enextToken(File *f); +static Token *enextToken(File *f, TokenType type); +static String evalType(String s); +static void g_miasto(File *f); static Token * -enextToken(File *f) +enextToken(File *f, TokenType type) { Token *r; if ((r = nextToken(f)) == NULL) errwarn(*f, 1, "unexpected end of input"); + if (type != TokenNULL && r->type != type) + errwarn(*f, 1, "token type mismatch (expected %s, have %s)", + stringizeTokenType(type), stringizeTokenType(r->type)); return r; } +static String +evalType(String s) +{ + if (!Strccmp(s, "literki")) + s.len = strlen(s.data = "char"); + if (!Strccmp(s, "cyferki")) + s.len = strlen(s.data = "int"); + if (!Strccmp(s, "latajace")) + s.len = strlen(s.data = "float"); + if (!Strccmp(s, "podwojne")) + s.len = strlen(s.data = "double"); + return s; +} + +static void +g_aglomeracja(File *f) +{ + Token *t; + + while ((t = enextToken(f, TokenNULL))) { + if (t->type == TokenIdentifier) { + if (!(Strccmp(t->c, "koniec"))) { + dprintf(f->outfd, "}\n"); + return; + } else { + errwarn(*f, 1, "unexpected identifier (expected a keyword)"); + } + } else { + errwarn(*f, 1, "unexpected token (expected an expression)"); + } + } + + return; +} + +static void +g_miasto(File *f) +{ + String name; + String type; + + Token *t; + + t = enextToken(f, TokenIdentifier); + name = t->c; + type.data = "int"; type.len = 3; + while ((t = enextToken(f, TokenNULL))) { + if (t->type == TokenColon) { + dprintf(f->outfd, "%.*s %.*s() {\n", + Strevalf(type), Strevalf(name)); + g_aglomeracja(f); + return; + } else if (t->type == TokenIdentifier) { + if (!(Strccmp(t->c, "oddaje"))) { + t = enextToken(f, TokenIdentifier); + type = evalType(t->c); + } else { + errwarn(*f, 1, "unexpected identifier (expected przyjmuje or oddaje)"); + } + } else { + errwarn(*f, 1, "unexpected token (expected colon, przyjmuje or oddaje)"); + } + } +} + void compile(File f) { Token *t; while ((t = nextToken(&f)) != NULL) { if (!(Strccmp(t->c, "wykorzystaj"))) { - t = enextToken(&f); + t = enextToken(&f, TokenString); dprintf(f.outfd, "#include <%.*s.h>\n", - (int)t->c.len, t->c.data); + (int)t->c.len - 2, t->c.data + 1); + } else if (!(Strccmp(t->c, "poczatek"))) { + t = enextToken(&f, TokenIdentifier); + if (!(Strccmp(t->c, "miasta"))) { + g_miasto(&f); + } } else { errwarn(f, 1, "unexpected token"); } diff --git a/include/str.h b/include/str.h @@ -31,4 +31,6 @@ typedef struct String { int Strcmp(String s1, String s2); int Strccmp(const String s, const char *cs); +#define Strevalf(S) (int)((S).len), (S).data + #endif