From c2fea1b48364f9bbb1af6dd83bfbb533a995bba1 Mon Sep 17 00:00:00 2001 From: Ewan Croft Date: Mon, 6 Apr 2026 19:52:48 +0100 Subject: [PATCH] Add lexer tokens and semantic checks for bitwise operators --- selenium/lexer.py | 7 ++++++- selenium/sema.py | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/selenium/lexer.py b/selenium/lexer.py index 816096d..eed2411 100644 --- a/selenium/lexer.py +++ b/selenium/lexer.py @@ -76,6 +76,11 @@ class Lexer: self._advance() self._advance() continue + if two in {"<<", ">>"}: + tokens.append(Token(two, two, start_line, start_col)) + self._advance() + self._advance() + continue if two in {"++", "--"}: tokens.append(Token(two, two, start_line, start_col)) self._advance() @@ -95,7 +100,7 @@ class Lexer: tokens.append(self._char()) continue - if ch in {";", ",", "(", ")", "{", "}", "+", "-", "*", "/", "%", "=", "<", ">", "!", ":", "?"}: + if ch in {";", ",", "(", ")", "{", "}", "+", "-", "*", "/", "%", "=", "<", ">", "!", ":", "?", "&", "|", "^"}: tokens.append(Token(ch, ch, start_line, start_col)) self._advance() continue diff --git a/selenium/sema.py b/selenium/sema.py index e2cdcb0..99f2c72 100644 --- a/selenium/sema.py +++ b/selenium/sema.py @@ -305,6 +305,11 @@ class SemanticAnalyzer: self._require_type(right, "bool", f"Operands of {op} must be bool") self.expr_types[id(expr)] = BUILTINS["bool"] return BUILTINS["bool"] + if op in {"<<", ">>", "&", "|", "^"}: + self._require_type(left, "int", f"Operands of {op} must be int") + self._require_type(right, "int", f"Operands of {op} must be int") + self.expr_types[id(expr)] = BUILTINS["int"] + return BUILTINS["int"] raise SemanticError(f"Unsupported binary operator: {op}") if isinstance(expr, Call): if expr.callee in {"read_int", "read_float", "read_bool", "read_char"}: -- 2.51.2