static AST* statement(Parser* p);
static AST* expression(Parser* p);
static AST* definition(Parser* p, bool constant);
+static AST* return_statement(Parser* p);
+static AST* if_statement(Parser* p);
static AST* func_expr_list(Parser* p);
static AST* const_expression(Parser* p);
} else {
if (matches(p, T_LET) || matches(p, T_VAR)) {
definition(p, (peek(p)->type == T_LET));
- } else if (accept(p, T_RETURN)) {
- expression(p);
- expect(p, ';');
-// } else if (matches(p, T_IF)) {
+ } else if (matches(p, T_RETURN)) {
+ return_statement(p);
+ } else if (matches(p, T_IF)) {
+ if_statement(p);
// } else if (matches(p, T_WHILE)) {
// } else if (matches(p, T_FOR)) {
// } else if (matches(p, T_DO)) {
return NULL;
}
+static AST* return_statement(Parser* p) {
+ expect(p, T_RETURN);
+ expression(p);
+ expect(p, ';');
+ return NULL;
+}
+
+static AST* if_statement(Parser* p) {
+ expect(p, T_IF);
+ expression(p);
+ expression_block(p);
+ if (accept(p, T_ELSE)) {
+ if (matches(p, T_IF)) {
+ if_statement(p);
+ } else {
+ expression_block(p);
+ }
+ }
+ return NULL;
+}
+
static AST* func_expr_list(Parser* p) {
expect(p, '(');
if (!matches(p, ')')) {
typedef enum {
T_NONE = 0, T_ERROR = 256, T_END_FILE,
T_PACKAGE, T_REQUIRES, T_PROVIDES, T_LET, T_VAR, T_FUN, T_TYPE, T_STRUCT,
- T_UNION, T_RETURN,
+ T_UNION, T_RETURN, T_IF, T_ELSE,
T_ID, T_CHAR, T_INT, T_FLOAT, T_BOOL, T_STRING,
} TokType;