raedr7n

parser skeleton for dice notation

Jun 13th, 2022 (edited)
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.46 KB | None | 0 0
  1. module randall.Parser
  2.  
  3. open System
  4.  
  5. type Token = AddT | SubT | MulT | DivT | ExpT | DT | GT | LT | OpT | CpT | NumT of int
  6.  
  7. let rec lex s =
  8.     if String.length s = 0 then [] else
  9.     match s.[0] with
  10.     | '+' -> AddT :: lex s.[1..]
  11.     | '-' -> SubT :: lex s.[1..]
  12.     | '*' -> MulT :: lex s.[1..]
  13.     | '/' -> DivT :: lex s.[1..]
  14.     | '^' -> ExpT :: lex s.[1..]
  15.     | 'd' -> DT :: lex s.[1..]
  16.     | 'g' -> GT :: lex s.[1..]
  17.     | 'l' -> LT :: lex s.[1..]
  18.     | '(' -> OpT :: lex s.[1..]
  19.     | ')' -> CpT :: lex s.[1..]
  20.     | d when d > '/' && d < ':' -> tokenize_num s
  21.     | c when c < '!' -> lex s.[1..]
  22.     | c -> failwith ("Illegal character: " + Char.ToString c)
  23. and tokenize_num s =
  24.     let rec select (s : string) n len =
  25.         if len = 0 then n else
  26.         if s.[0] > '/' && s.[0] < ':' then
  27.             select s.[1..] (n + 1) (len - 1)
  28.         else n
  29.     in
  30.     let fin = select s 0 (String.length s) in
  31.     NumT (int s.[..fin - 1]) :: lex s.[fin..]
  32.            
  33. type Rpg0  = Single of Rpg1 * Rpg01
  34.            | Debug
  35. and Rpg01  = Add of Rpg1 * Rpg01
  36.            | Sub of Rpg1 * Rpg01
  37.            | Nil
  38.            | Debug
  39. and Rpg1   = Single of Rpg2 * Rpg11
  40.            | Debug
  41. and Rpg11  = Mul of Rpg2 * Rpg11
  42.            | Div of Rpg2 * Rpg11
  43.            | Nil
  44.            | Debug
  45. and Rpg2   = Single of All * Rpg21
  46.            | Debug
  47. and Rpg21  = Exp of All * Rpg21
  48.            | Nil
  49.            | Debug
  50. and All    = A of Loop
  51.            | B of Loop * Loop
  52.            | Next of G_Dice
  53.            | Debug
  54. and G_Dice = A of Loop
  55.            | B of Loop * Loop
  56.            | C of Loop * Loop
  57.            | D of Loop * Loop * Loop
  58.            | Next of L_Dice
  59.            | Debug
  60. and L_Dice = A of Loop
  61.            | B of Loop * Loop
  62.            | C of Loop * Loop
  63.            | D of Loop * Loop * Loop
  64.            | Next of Loop
  65.            | Debug
  66. and Loop   = Num of int
  67.            | Closed of Rpg0
  68.            | Debug
  69.  
  70. let rpg0 (t : Token list ref) : Rpg0 = Rpg0.Debug
  71. let rpg01 (t : Token list ref) : Rpg01 = Rpg01.Debug
  72. let rpg1 (t : Token list ref) : Rpg1 = Rpg1.Debug
  73. let rpg11 (t : Token list ref) : Rpg11 = Rpg11.Debug
  74. let rpg2 (t : Token list ref) : Rpg2 = Rpg2.Debug
  75. let rpg21 (t : Token list ref) : Rpg21 = Rpg21.Debug
  76. let all (t : Token list ref) : All = All.Debug
  77. let g_dice (t : Token list ref) : G_Dice = G_Dice.Debug
  78. let l_dice (t : Token list ref) : L_Dice = L_Dice.Debug
  79. let loop (t : Token list ref) : Loop = Loop.Debug
  80.  
Add Comment
Please, Sign In to add comment