umx_compiler

UMX virtual machine "Monkey" interpreter / bytecode compiler
git clone git://bsandro.tech/umx_compiler
Log | Files | Refs | README | LICENSE

lexer_test.go (3040B)


      1 package lexer
      2 
      3 import (
      4 	"umx_compiler/token"
      5 	"testing"
      6 )
      7 
      8 func TestNextToken(t *testing.T) {
      9 	input := `let five = 5;
     10 let ten = 10;
     11 let add = fn(x, y) {
     12   x + y;
     13 };
     14 let result = add(five, ten);
     15 !-*/5;
     16 6 < 10 > 8;
     17 if (5 < 10) {
     18   return true;
     19 } else {
     20   return false;
     21 }
     22 
     23 10 == 10;
     24 10 != 8;
     25 
     26 "foo123";
     27 "foo bar";
     28 
     29 [1, 2];
     30 
     31 {"foo":"bar"};
     32 
     33 let myvar_1 = 1;
     34 `
     35 
     36 	tests := []struct {
     37 		expectedType    token.TokenType
     38 		expectedLiteral string
     39 	}{
     40 		{token.LET, "let"},
     41 		{token.IDENT, "five"},
     42 		{token.ASSIGN, "="},
     43 		{token.INT, "5"},
     44 		{token.SEMICOLON, ";"},
     45 		{token.LET, "let"},
     46 		{token.IDENT, "ten"},
     47 		{token.ASSIGN, "="},
     48 		{token.INT, "10"},
     49 		{token.SEMICOLON, ";"},
     50 		{token.LET, "let"},
     51 		{token.IDENT, "add"},
     52 		{token.ASSIGN, "="},
     53 		{token.FUNCTION, "fn"},
     54 		{token.LPAREN, "("},
     55 		{token.IDENT, "x"},
     56 		{token.COMMA, ","},
     57 		{token.IDENT, "y"},
     58 		{token.RPAREN, ")"},
     59 		{token.LCURLY, "{"},
     60 		{token.IDENT, "x"},
     61 		{token.PLUS, "+"},
     62 		{token.IDENT, "y"},
     63 		{token.SEMICOLON, ";"},
     64 		{token.RCURLY, "}"},
     65 		{token.SEMICOLON, ";"},
     66 		{token.LET, "let"},
     67 		{token.IDENT, "result"},
     68 		{token.ASSIGN, "="},
     69 		{token.IDENT, "add"},
     70 		{token.LPAREN, "("},
     71 		{token.IDENT, "five"},
     72 		{token.COMMA, ","},
     73 		{token.IDENT, "ten"},
     74 		{token.RPAREN, ")"},
     75 		{token.SEMICOLON, ";"},
     76 
     77 		{token.SHRIEK, "!"},
     78 		{token.MINUS, "-"},
     79 		{token.ASTERISK, "*"},
     80 		{token.SLASH, "/"},
     81 		{token.INT, "5"},
     82 		{token.SEMICOLON, ";"},
     83 		{token.INT, "6"},
     84 		{token.LT, "<"},
     85 		{token.INT, "10"},
     86 		{token.GT, ">"},
     87 		{token.INT, "8"},
     88 		{token.SEMICOLON, ";"},
     89 
     90 		{token.IF, "if"},
     91 		{token.LPAREN, "("},
     92 		{token.INT, "5"},
     93 		{token.LT, "<"},
     94 		{token.INT, "10"},
     95 		{token.RPAREN, ")"},
     96 		{token.LCURLY, "{"},
     97 		{token.RETURN, "return"},
     98 		{token.TRUE, "true"},
     99 		{token.SEMICOLON, ";"},
    100 		{token.RCURLY, "}"},
    101 		{token.ELSE, "else"},
    102 		{token.LCURLY, "{"},
    103 		{token.RETURN, "return"},
    104 		{token.FALSE, "false"},
    105 		{token.SEMICOLON, ";"},
    106 		{token.RCURLY, "}"},
    107 
    108 		{token.INT, "10"},
    109 		{token.EQUAL, "=="},
    110 		{token.INT, "10"},
    111 		{token.SEMICOLON, ";"},
    112 		{token.INT, "10"},
    113 		{token.NOT_EQUAL, "!="},
    114 		{token.INT, "8"},
    115 		{token.SEMICOLON, ";"},
    116 
    117 		{token.STRING, "foo123"},
    118 		{token.SEMICOLON, ";"},
    119 		{token.STRING, "foo bar"},
    120 		{token.SEMICOLON, ";"},
    121 
    122 		{token.LBRACKET, "["},
    123 		{token.INT, "1"},
    124 		{token.COMMA, ","},
    125 		{token.INT, "2"},
    126 		{token.RBRACKET, "]"},
    127 		{token.SEMICOLON, ";"},
    128 
    129 		{token.LCURLY, "{"},
    130 		{token.STRING, "foo"},
    131 		{token.COLON, ":"},
    132 		{token.STRING, "bar"},
    133 		{token.RCURLY, "}"},
    134 		{token.SEMICOLON, ";"},
    135 
    136 		{token.LET, "let"},
    137 		{token.IDENT, "myvar_1"},
    138 		{token.ASSIGN, "="},
    139 		{token.INT, "1"},
    140 		{token.SEMICOLON, ";"},
    141 
    142 		{token.EOF, ""},
    143 	}
    144 
    145 	l := New(input)
    146 
    147 	for i, tt := range tests {
    148 		tok := l.NextToken()
    149 		if tok.Type != tt.expectedType {
    150 			t.Fatalf("tests[%d] error: token type is wrong, expected %q, got %q", i, tt.expectedType, tok.Type)
    151 		}
    152 		if tok.Literal != tt.expectedLiteral {
    153 			t.Fatalf("tests[%d] error: literal is wrong, expected %q, got %q", i, tt.expectedLiteral, tok.Literal)
    154 		}
    155 	}
    156 }