diff --git a/ast/parser.go b/ast/parser.go index d6e0ace..909e713 100644 --- a/ast/parser.go +++ b/ast/parser.go @@ -370,7 +370,7 @@ func (p *Parser) unary() Expression { func (p *Parser) multiplication() Expression { expr := p.unary() - for p.match(tokens.SLASH, tokens.STAR, tokens.PERCENT) { + for p.match(tokens.SLASH, tokens.STAR, tokens.PERCENT, tokens.ENUMOF) { expr = BinaryExpression{ Left: expr, Operator: p.previous(), diff --git a/examples/enums.u b/examples/enums.u index 336fbaf..62aa00d 100644 --- a/examples/enums.u +++ b/examples/enums.u @@ -26,6 +26,9 @@ for mut i num = 0, ~events - 1 { } } +io::println(Event.Keyboard("a") enumof Event.Keyboard) # true +io::println(Event.Keyboard("a") == Event.Keyboard("a")) # true +io::println(Event.Keyboard("a") == Event.Keyboard("b")) # false ## Todo # - [x] Declare enums diff --git a/exception/messages.go b/exception/messages.go index 1949b76..f000f1f 100644 --- a/exception/messages.go +++ b/exception/messages.go @@ -38,6 +38,8 @@ var Messages = map[string]string{ "RT034": "enum member '%s' does not exits in '%s'", "RT035": "cannot use '%s' as a type", "RT036": "enum member expects to be called with arguments", + "RT037": "left operand in enumof should be an enum member", + "RT038": "right operand in enumof should be an enum member", "GN001": "cannot find module '%s'", "GN002": "unable to load file '%s'. module does not exits. path: %s", } diff --git a/interpreter/eval.go b/interpreter/eval.go index b7825e7..5b1bceb 100644 --- a/interpreter/eval.go +++ b/interpreter/eval.go @@ -255,6 +255,17 @@ func Evaluate(expression ast.Expression, env *environment.Environment) (interfac } case tokens.BANG_EQUAL: return left != right, nil + case tokens.ENUMOF: + leftVal, ok := left.(ast.EnumMember) + if !ok { + return nil, exception.NewRuntimeError("RT037") + } + rightVal, ok := right.(ast.EnumMember) + if !ok { + return nil, exception.NewRuntimeError("RT038") + } + + return leftVal.Signature == rightVal.Signature && leftVal.Name == rightVal.Name, nil default: return nil, exception.NewRuntimeError("RT010", expr.Operator.Lexeme) } diff --git a/tokens/tokens.go b/tokens/tokens.go index 9ca6d3e..8fde39c 100644 --- a/tokens/tokens.go +++ b/tokens/tokens.go @@ -70,6 +70,7 @@ const ( ENUM TokenType = "ENUM" MATCH TokenType = "MATCH" PIPE TokenType = "PIPE" + ENUMOF TokenType = "ENUMOF" ) var PRIMITIVE_TYPES = []TokenType{ @@ -128,6 +129,7 @@ var reservedKeywordsMap = map[string]TokenType{ "typeof": TYPE_OF, "enum": ENUM, "match": MATCH, + "enumof": ENUMOF, } func getKeyword(lexis string) TokenType {