static AST* const_expression(Parser* p);
static AST* definition(Parser* p);
static AST* expression(Parser* p);
+static AST* identifier(Parser* p);
+
static AST* function(Parser* p);
static void type_annotation(Parser* p);
static AST* literal(Parser* p);
expr = const_expression(p);
expect(p, T_RPAR);
} else if (match(p, T_ID)) {
- expr = Ident(expect_val(p, T_ID));
+ expr = identifier(p);
} else {
expr = literal(p);
}
case T_STRING:
case T_INT:
case T_FLOAT:
- ret = token_to_tree(expect_val(p, tok->type));
+ ret = token_to_tree(tok);
+ tok->type = T_NONE;
break;
default:
error(p, "Expected a literal");
}
}
+static AST* identifier(Parser* p) {
+ Tok* tok = peek(p);
+ if (tok->type == T_ID) {
+ AST* ast = Ident(tok);
+ tok->type = T_NONE;
+ return ast;
+ } else {
+ error(p, "Expected an identifier");
+ return NULL;
+ }
+}
+
#if 0
static AST* definition(Parser* p) {
it "should recognize 'c'" do
expect(lexer('\c')).to eq ['T_CHAR:\c']
end
-
- it "should recognize invalid named characters as identifiers" do
- expect(lexer('\foobar')).to eq ['T_ID:\foobar']
- end
end
context "numbers" do
it "should recognize negitve integer with sign" do
expect(lexer('-123')).to eq ['T_INT:-123']
end
-
- it "should recognize invalid ints as identifiers" do
- expect(lexer('123a')).to eq ['T_ID:123a']
- end
end
context "radix integers" do
it "should recognize decimal integer" do
expect(lexer('0hf0f')).to eq ['T_INT:3855']
end
-
- it "should recognize invalid radix ints as identifiers" do
- expect(lexer('0b012')).to eq ['T_ID:0b012']
- end
end
context "floating point" do
it "should recognize negative float with sign" do
expect(lexer('-123.0')).to eq ['T_FLOAT:-123.000000']
end
-
- it "should recognize invalid floats as identifiers" do
- expect(lexer('123..0')).to eq ['T_ID:123..0']
- end
end
end