static AST* const_definition(Parser* p, bool constant);
static AST* type_definition(Parser* p);
static AST* func_definition(Parser* p);
+static AST* type_expression(Parser* p);
static AST* const_expression(Parser* p);
static AST* definition(Parser* p);
static AST* expression(Parser* p);
exit(1);
}
-static bool match(Parser* parser, TokType type) {
- return (peek(parser)->type == type);
+static bool matches(Parser* p, TokType type) {
+ return (peek(p)->type == type);
}
-static bool accept(Parser* parser, TokType type) {
- if (peek(parser)->type == type) {
- parser->tok.type = T_NONE;
+static bool accept(Parser* p, TokType type) {
+ if (matches(p, type)) {
+ p->tok.type = T_NONE;
return true;
}
return false;
}
-static void expect(Parser* parser, TokType type) {
- if (!accept(parser, type))
- error(parser, "Unexpected token");
+static void expect(Parser* p, TokType type) {
+ if (!accept(p, type))
+ error(p, "Unexpected token");
}
-static Tok* expect_val(Parser* parser, TokType type) {
+static Tok* expect_val(Parser* p, TokType type) {
Tok* tok = NULL;
- if (peek(parser)->type == type) {
+ if (matches(p, type)) {
tok = calloc(1, sizeof(Tok));
- *tok = *(peek(parser));
- parser->tok.type = T_NONE;
+ *tok = *(peek(p));
+ p->tok.type = T_NONE;
} else {
- error(parser, "Unexpected token");
+ error(p, "Unexpected token");
}
return tok;
}
AST* toplevel(Parser* p) {
expect(p, T_PACKAGE);
expect(p, T_ID);
- if (accept(p, T_REQUIRES))
+ if (accept(p, T_REQUIRES)) {
require_list(p);
- if (accept(p, T_PROVIDES))
+ }
+ if (accept(p, T_PROVIDES)) {
provide_list(p);
+ }
definition_list(p);
return NULL;
}
static void require_list(Parser* p) {
expect(p, T_LPAR);
- while (peek(p)->type != T_RPAR) {
+ while (!matches(p, T_RPAR)) {
expect(p, T_STRING);
}
expect(p, T_RPAR);
static void provide_list(Parser* p) {
expect(p, T_LPAR);
- while (peek(p)->type != T_RPAR) {
+ while (!matches(p, T_RPAR)) {
expect(p, T_ID);
}
expect(p, T_RPAR);
}
static AST* definition_list(Parser* p) {
- while (!match(p, T_END_FILE)) {
+ while (!matches(p, T_END_FILE)) {
TokType type = peek(p)->type;
if (accept(p, T_LET) || accept(p, T_VAR)) {
const_definition(p, (type == T_LET));
}
static AST* type_definition(Parser* p) {
+ expect(p, T_TYPE);
+ expect(p, T_ID);
+ type_expression(p);
return NULL;
}
static AST* func_definition(Parser* p) {
return NULL;
-
}
static AST* const_expression(Parser* p) {
if (accept(p, T_LPAR)) {
expr = const_expression(p);
expect(p, T_RPAR);
- } else if (match(p, T_ID)) {
+ } else if (matches(p, T_ID)) {
expr = identifier(p);
} else {
expr = literal(p);
return expr;
}
+static AST* type_expression(Parser* p) {
+ expect(p, T_ID);
+ return NULL;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
static Type* get_typedef(Parser* p, char* typename) {
Sym* sym = sym_get(&(p->syms), typename);
if (!sym) error(p, "unknown type '%s'", typename);
T_PACKAGE, T_REQUIRES, T_PROVIDES, T_LET, T_VAR, T_FUN, T_TYPE,
T_ID, T_CHAR, T_INT, T_FLOAT, T_BOOL, T_STRING,
T_LBRACE, T_RBRACE, T_LBRACK, T_RBRACK, T_LPAR, T_RPAR, T_COMMA, T_SQUOTE,
- T_DQUOTE, T_END, T_COLON, T_AMP, T_IF, T_THEN, T_ELSE, T_ASSIGN
+ T_DQUOTE, T_COLON, T_AMP, T_IF, T_ELSE, T_ASSIGN
} TokType;
typedef struct {
expect(lexer(',')).to eq ['T_COMMA']
end
- it "should recognize ;" do
- expect(lexer(';')).to eq ['T_END']
- end
-
it "should recognize :" do
expect(lexer(':')).to eq ['T_COLON']
end
it "should recognize all punctuation" do
- expect(lexer('[](){}\',;')).to eq(
+ expect(lexer('[](){}\',')).to eq(
["T_LBRACK", "T_RBRACK", "T_LPAR", "T_RPAR", "T_LBRACE", "T_RBRACE",
- "T_SQUOTE", "T_COMMA", "T_END"])
+ "T_SQUOTE", "T_COMMA"])
end
it "should recognize [ after an identifier" do
end
it "should recognize } after an identifier" do
- expect(lexer('foo;')).to eq(['T_ID:foo', 'T_END'])
+ expect(lexer('foo')).to eq(['T_ID:foo'])
end
end