progs/lexer/toks.scala
changeset 729 b147a10be8dd
parent 728 c669b39debe3
child 730 18fee9d3b6a8
equal deleted inserted replaced
728:c669b39debe3 729:b147a10be8dd
     1 abstract class Token
       
     2 case object T_SEMI extends Token
       
     3 case object T_LPAREN extends Token
       
     4 case object T_RPAREN extends Token
       
     5 case class T_ID(s: String) extends Token
       
     6 case class T_OP(s: String) extends Token
       
     7 case class T_NUM(n: Int) extends Token
       
     8 case class T_KWD(s: String) extends Token
       
     9 case class T_STR(s: String) extends Token
       
    10 
       
    11 
       
    12 // Loops program
       
    13 //===============
       
    14 
       
    15 /*
       
    16 start := 1000; 
       
    17 x := start;
       
    18 y := start;
       
    19 z := start;
       
    20 while 0 < x do {
       
    21  while 0 < y do {
       
    22   while 0 < z do { z := z - 1 };
       
    23   z := start;
       
    24   y := y - 1
       
    25  };     
       
    26  y := start;
       
    27  x := x - 1
       
    28 }
       
    29 */
       
    30 
       
    31 val loops =
       
    32   List(T_ID("start"), T_OP(":="), T_NUM(1000), T_SEMI, T_ID("x"), T_OP(":="), 
       
    33        T_ID("start"), T_SEMI, T_ID("y"), T_OP(":="), T_ID("start"), T_SEMI, 
       
    34        T_ID("z"), T_OP(":="), T_ID("start"), T_SEMI, T_KWD("while"), T_NUM(0), 
       
    35        T_OP("<"), T_ID("x"), T_KWD("do"), T_LPAREN, T_KWD("while"), T_NUM(0), 
       
    36        T_OP("<"), T_ID("y"), T_KWD("do"), T_LPAREN, T_KWD("while"), T_NUM(0), 
       
    37        T_OP("<"), T_ID("z"), T_KWD("do"), T_LPAREN, T_ID("z"), T_OP(":="), 
       
    38        T_ID("z"), T_OP("-"), T_NUM(1), T_RPAREN, T_SEMI, T_ID("z"), T_OP(":="),
       
    39        T_ID("start"), T_SEMI, T_ID("y"), T_OP(":="), T_ID("y"), T_OP("-"), 
       
    40        T_NUM(1), T_RPAREN, T_SEMI, T_ID("y"), T_OP(":="), T_ID("start"), 
       
    41        T_SEMI, T_ID("x"), T_OP(":="), T_ID("x"), T_OP("-"), T_NUM(1), T_RPAREN) 
       
    42 
       
    43 
       
    44 // Fib program
       
    45 //=============
       
    46 
       
    47 /*
       
    48 write "Fib";
       
    49 read n;  
       
    50 minus1 := 0;
       
    51 minus2 := 1;
       
    52 while n > 0 do {
       
    53        temp := minus2;
       
    54        minus2 := minus1 + minus2;
       
    55        minus1 := temp;
       
    56        n := n - 1
       
    57 };
       
    58 write "Result";
       
    59 write minus2
       
    60 */
       
    61 
       
    62 val fib =
       
    63   List(T_KWD("write"), T_STR("Fib"), T_SEMI, T_KWD("read"), T_ID("n"), 
       
    64        T_SEMI, T_ID("minus1"), T_OP(":="), T_NUM(0), T_SEMI, T_ID("minus2"), 
       
    65        T_OP(":="), T_NUM(1), T_SEMI, T_KWD("while"), T_ID("n"), T_OP(">"), 
       
    66        T_NUM(0), T_KWD("do"), T_LPAREN, T_ID("temp"), T_OP(":="), 
       
    67        T_ID("minus2"), T_SEMI, T_ID("minus2"), T_OP(":="), T_ID("minus1"), 
       
    68        T_OP("+"), T_ID("minus2"), T_SEMI, T_ID("minus1"), T_OP(":="), 
       
    69        T_ID("temp"), T_SEMI, T_ID("n"), T_OP(":="), T_ID("n"), T_OP("-"), 
       
    70        T_NUM(1), T_RPAREN, T_SEMI, T_KWD("write"), T_STR("Result"), T_SEMI, 
       
    71        T_KWD("write"), T_ID("minus2"))
       
    72 
       
    73 
       
    74 
       
    75 // Factors program
       
    76 //=================
       
    77 
       
    78 /*
       
    79 write "Input n please";
       
    80 read n;
       
    81 write "The factors of n are";
       
    82 f := 2;
       
    83 while n != 1 do {
       
    84     while (n / f) * f == n do {
       
    85         write f;
       
    86         n := n / f
       
    87     };
       
    88     f := f + 1
       
    89 }
       
    90 */
       
    91 
       
    92 val factors = 
       
    93   List(T_KWD("write"), T_STR("Input n please"), T_SEMI, T_KWD("read"), 
       
    94        T_ID("n"), T_SEMI, T_KWD("write"), T_STR("The factors of n are"), 
       
    95        T_SEMI, T_ID("f"), T_OP(":="), T_NUM(2), T_SEMI, T_KWD("while"), 
       
    96        T_ID("n"), T_OP("!="), T_NUM(1), T_KWD("do"), T_LPAREN, 
       
    97        T_KWD("while"), T_ID("n"), T_OP("/"), T_ID("f"), T_OP("*"), 
       
    98        T_ID("f"), T_OP("=="), T_ID("n"), T_KWD("do"), T_LPAREN, 
       
    99        T_KWD("write"), T_ID("f"), T_SEMI, T_ID("n"), T_OP(":="), 
       
   100        T_ID("n"), T_OP("/"), T_ID("f"), T_RPAREN, T_SEMI, T_ID("f"), 
       
   101        T_OP(":="), T_ID("f"), T_OP("+"), T_NUM(1), T_RPAREN)
       
   102 
       
   103 
       
   104 // Primes program
       
   105 //================
       
   106 
       
   107 /*
       
   108 end := 100;
       
   109 n := 2;
       
   110 while (n < end) do {
       
   111   f := 2;
       
   112   tmp := 0;
       
   113   while ((f < n / 2 + 1) && (tmp == 0)) do {
       
   114     if ((n / f) * f == n) then  { tmp := 1 } else { skip };
       
   115     f := f + 1
       
   116   };
       
   117   if (tmp == 0) then { write(n) } else { skip };
       
   118   n  := n + 1
       
   119 }
       
   120 */
       
   121 
       
   122 val primes =
       
   123   List(T_ID("end"), T_OP(":="), T_NUM(100), T_SEMI, T_ID("n"), T_OP(":="), 
       
   124        T_NUM(2), T_SEMI, T_KWD("while"), T_ID("n"), T_OP("<"), T_ID("end"), 
       
   125        T_KWD("do"), T_LPAREN, T_ID("f"), T_OP(":="), T_NUM(2), T_SEMI, 
       
   126        T_ID("tmp"), T_OP(":="), T_NUM(0), T_SEMI, T_KWD("while"), T_ID("f"), 
       
   127        T_OP("<"), T_ID("n"), T_OP("/"), T_NUM(2), T_OP("+"), T_NUM(1), 
       
   128        T_OP("&&"), T_ID("tmp"), T_OP("=="), T_NUM(0), T_KWD("do"), T_LPAREN, 
       
   129        T_KWD("if"), T_ID("n"), T_OP("/"), T_ID("f"), T_OP("*"), T_ID("f"), 
       
   130        T_OP("=="), T_ID("n"), T_KWD("then"), T_LPAREN, T_ID("tmp"), T_OP(":="),
       
   131        T_NUM(1), T_RPAREN, T_KWD("else"), T_LPAREN, T_KWD("skip"), T_RPAREN, 
       
   132        T_SEMI, T_ID("f"), T_OP(":="), T_ID("f"), T_OP("+"), T_NUM(1), 
       
   133        T_RPAREN, T_SEMI, T_KWD("if"), T_ID("tmp"), T_OP("=="), T_NUM(0), 
       
   134        T_KWD("then"), T_LPAREN, T_KWD("write"), T_ID("n"), T_RPAREN, 
       
   135        T_KWD("else"), T_LPAREN, T_KWD("skip"), T_RPAREN, T_SEMI, T_ID("n"), 
       
   136        T_OP(":="), T_ID("n"), T_OP("+"), T_NUM(1), T_RPAREN)