diff --git a/src/lexer.l b/src/lexer.l index 3e6e9c7..21f0bd3 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -47,6 +47,8 @@ and { return yy::parser::token::TOK_KW_AND; } or { return yy::parser::token::TOK_KW_OR; } pass { return yy::parser::token::TOK_KW_PASS; } else { return yy::parser::token::TOK_KW_ELSE; } +elif { return yy::parser::token::TOK_KW_ELIF; } +class { return yy::parser::token::TOK_KW_CLASS; } \=\= { return yy::parser::token::TOK_EQ; } \!\= { return yy::parser::token::TOK_NE; } diff --git a/src/parser.y b/src/parser.y index ce2e80b..f2f836e 100644 --- a/src/parser.y +++ b/src/parser.y @@ -28,7 +28,7 @@ %token STRING %token NUMBER %token ASSIGN PLUS MINUS L_PAREN R_PAREN COLON EQ NE LT GT LTE GTE MULTIPLY DIVIDE MODULO L_BRACKET L_BRACE R_BRACKET R_BRACE DOT SEMICOLON COMMA BIT_NOT BIT_AND BIT_OR BIT_XOR LSHIFT RSHIFT PLUS_ASSIGN MINUS_ASSIGN MULTIPLY_ASSIGN DIVIDE_ASSIGN MODULO_ASSIGN BIT_AND_ASSIGN BIT_OR_ASSIGN BIT_XOR_ASSIGN LSHIFT_ASSIGN RSHIFT_ASSIGN -%token KW_DEF KW_IF KW_RETURN KW_FOR KW_IN KW_NOT KW_AND KW_OR KW_PASS KW_ELSE +%token KW_DEF KW_IF KW_RETURN KW_FOR KW_IN KW_NOT KW_AND KW_OR KW_PASS KW_ELSE KW_ELIF KW_CLASS %token EOF NEWLINE INDENT DEDENT diff --git a/test/lexer/16.in b/test/lexer/16.in new file mode 100644 index 0000000..14477b0 --- /dev/null +++ b/test/lexer/16.in @@ -0,0 +1 @@ +x = 1<<2>>3&4|5^6 diff --git a/test/lexer/16.out b/test/lexer/16.out new file mode 100644 index 0000000..8c4f25a --- /dev/null +++ b/test/lexer/16.out @@ -0,0 +1,15 @@ +IDENTIFIER x +ASSIGN +NUMBER 1 +LSHIFT +NUMBER 2 +RSHIFT +NUMBER 3 +BIT_AND +NUMBER 4 +BIT_OR +NUMBER 5 +BIT_XOR +NUMBER 6 +NEWLINE +EOF \ No newline at end of file diff --git a/test/lexer/17.in b/test/lexer/17.in new file mode 100644 index 0000000..b2ea3f2 --- /dev/null +++ b/test/lexer/17.in @@ -0,0 +1,3 @@ +s1 = '"double inside"' +s2 = "'single inside'" +s3 = "mixed 'quotes' \"inside\"" diff --git a/test/lexer/17.out b/test/lexer/17.out new file mode 100644 index 0000000..ce93f0e --- /dev/null +++ b/test/lexer/17.out @@ -0,0 +1,13 @@ +IDENTIFIER s1 +ASSIGN +STRING '"double inside"' +NEWLINE +IDENTIFIER s2 +ASSIGN +STRING "'single inside'" +NEWLINE +IDENTIFIER s3 +ASSIGN +STRING "mixed 'quotes' \"inside\"" +NEWLINE +EOF \ No newline at end of file diff --git a/test/lexer/18.in b/test/lexer/18.in new file mode 100644 index 0000000..28db3f1 --- /dev/null +++ b/test/lexer/18.in @@ -0,0 +1 @@ +data = [{"a": [1, 2, 3]}, (4, 5), {6, 7}] diff --git a/test/lexer/18.out b/test/lexer/18.out new file mode 100644 index 0000000..7a1502e --- /dev/null +++ b/test/lexer/18.out @@ -0,0 +1,29 @@ +IDENTIFIER data +ASSIGN +L_BRACKET +L_BRACE +STRING "a" +COLON +L_BRACKET +NUMBER 1 +COMMA +NUMBER 2 +COMMA +NUMBER 3 +R_BRACKET +R_BRACE +COMMA +L_PAREN +NUMBER 4 +COMMA +NUMBER 5 +R_PAREN +COMMA +L_BRACE +NUMBER 6 +COMMA +NUMBER 7 +R_BRACE +R_BRACKET +NEWLINE +EOF \ No newline at end of file diff --git a/test/lexer/19.in b/test/lexer/19.in new file mode 100644 index 0000000..7e50ca2 --- /dev/null +++ b/test/lexer/19.in @@ -0,0 +1,25 @@ +def process(items): + result = [] + for i, item in enumerate(items): + if item > 0: + result.append(item * 2) + elif item < 0: + result.append(-item) + else: + pass + total = sum(result) + return total + +class Calculator: + def __init__(self, base=10): + self.base = base + + def add(self, x, y): + return x + y + + def multiply(self, x, y): + return x * y + +x = process([1, -2, 0]) +calc = Calculator() +sum_result = calc.add(5, 15) diff --git a/test/lexer/19.out b/test/lexer/19.out new file mode 100644 index 0000000..36a87a5 --- /dev/null +++ b/test/lexer/19.out @@ -0,0 +1,170 @@ +KW_DEF +IDENTIFIER process +L_PAREN +IDENTIFIER items +R_PAREN +COLON +NEWLINE +INDENT +IDENTIFIER result +ASSIGN +L_BRACKET +R_BRACKET +NEWLINE +KW_FOR +IDENTIFIER i +COMMA +IDENTIFIER item +KW_IN +IDENTIFIER enumerate +L_PAREN +IDENTIFIER items +R_PAREN +COLON +NEWLINE +INDENT +KW_IF +IDENTIFIER item +GT +NUMBER 0 +COLON +NEWLINE +INDENT +IDENTIFIER result +DOT +IDENTIFIER append +L_PAREN +IDENTIFIER item +MULTIPLY +NUMBER 2 +R_PAREN +NEWLINE +DEDENT +KW_ELIF +IDENTIFIER item +LT +NUMBER 0 +COLON +NEWLINE +INDENT +IDENTIFIER result +DOT +IDENTIFIER append +L_PAREN +MINUS +IDENTIFIER item +R_PAREN +NEWLINE +DEDENT +KW_ELSE +COLON +NEWLINE +INDENT +KW_PASS +NEWLINE +DEDENT +DEDENT +IDENTIFIER total +ASSIGN +IDENTIFIER sum +L_PAREN +IDENTIFIER result +R_PAREN +NEWLINE +KW_RETURN +IDENTIFIER total +NEWLINE +DEDENT +KW_CLASS +IDENTIFIER Calculator +COLON +NEWLINE +INDENT +KW_DEF +IDENTIFIER __init__ +L_PAREN +IDENTIFIER self +COMMA +IDENTIFIER base +ASSIGN +NUMBER 10 +R_PAREN +COLON +NEWLINE +INDENT +IDENTIFIER self +DOT +IDENTIFIER base +ASSIGN +IDENTIFIER base +NEWLINE +DEDENT +KW_DEF +IDENTIFIER add +L_PAREN +IDENTIFIER self +COMMA +IDENTIFIER x +COMMA +IDENTIFIER y +R_PAREN +COLON +NEWLINE +INDENT +KW_RETURN +IDENTIFIER x +PLUS +IDENTIFIER y +NEWLINE +DEDENT +KW_DEF +IDENTIFIER multiply +L_PAREN +IDENTIFIER self +COMMA +IDENTIFIER x +COMMA +IDENTIFIER y +R_PAREN +COLON +NEWLINE +INDENT +KW_RETURN +IDENTIFIER x +MULTIPLY +IDENTIFIER y +NEWLINE +DEDENT +DEDENT +IDENTIFIER x +ASSIGN +IDENTIFIER process +L_PAREN +L_BRACKET +NUMBER 1 +COMMA +MINUS +NUMBER 2 +COMMA +NUMBER 0 +R_BRACKET +R_PAREN +NEWLINE +IDENTIFIER calc +ASSIGN +IDENTIFIER Calculator +L_PAREN +R_PAREN +NEWLINE +IDENTIFIER sum_result +ASSIGN +IDENTIFIER calc +DOT +IDENTIFIER add +L_PAREN +NUMBER 5 +COMMA +NUMBER 15 +R_PAREN +NEWLINE +EOF \ No newline at end of file