Ast.fs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. module QSAST
  2. type Value =
  3. | Int of int
  4. | String of string
  5. type Op =
  6. // "+" | "-" | "*" | "/" | "mod"
  7. | Plus | Minus | Times | Divide | Mod
  8. // "=" | ">" | ">=" | "<" | "<=" | ("!" | "<>")| =< | =>
  9. | Eq | Gt | Ge | Lt | Le | Ne | El | Eg
  10. // "and" | "or"
  11. | And | Or
  12. [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
  13. [<RequireQualifiedAccess>]
  14. module Op =
  15. [<ReflectedDefinition>]
  16. let toString = function
  17. | Times -> "*"
  18. | Divide -> "/"
  19. | Mod -> "mod"
  20. | Plus -> "+"
  21. | Minus -> "-"
  22. | Lt -> "<"
  23. | Gt -> ">"
  24. | Le -> "<="
  25. | Eg -> "=>"
  26. | Ge -> ">="
  27. | El -> "=<"
  28. | Ne -> "<>"
  29. | And -> "and"
  30. | Or -> "or"
  31. | Eq -> "="
  32. // opp.AddOperator(InfixOperator("=>", ws, 4, A.Left, fun x y -> Expr(Ge, x, y)))
  33. // opp.AddOperator(InfixOperator("=<", ws, 4, A.Left, fun x y -> Expr(Le, x, y)))
  34. let ops = Reflect.unionCaseToList <@toString@>
  35. let fromString =
  36. let m = Map.ofList <| List.map (fun (a,b) -> b,a) ops
  37. fun x -> match Map.tryFind x m with Some x -> x | None -> failwithf "not found %A" x
  38. type UnarOp =
  39. // "-" |"obj" | "no"
  40. | Neg | Obj | No
  41. [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
  42. [<RequireQualifiedAccess>]
  43. module UnarOp =
  44. [<ReflectedDefinition>]
  45. let toString = function | Obj -> "obj" | Neg -> "-" | No -> "no"
  46. let ops = Reflect.unionCaseToList <@toString@>
  47. let fromString =
  48. let m = Map.ofList <| List.map (fun (a,b) -> b,a) ops
  49. fun x -> match Map.tryFind x m with Some x -> x | None -> failwithf "not found %A" x
  50. module Precedences =
  51. type T = OpB of Op | PrefB of UnarOp
  52. //&
  53. //OR
  54. //AND
  55. //OBJ, NO
  56. //=, <, >, !, <>, <=, >=, =<, =>
  57. //+, -
  58. //MOD
  59. //*, /
  60. //+, - (унарные)
  61. // "=, <, >, <>, <=, >=".Split([|", "|], System.StringSplitOptions.None)
  62. // |> Array.map (Op.fromString >> sprintf "OpB %A") |> join " | "
  63. let prec = function
  64. | OpB Or -> 1
  65. | OpB And -> 2
  66. | PrefB Obj | PrefB No -> 3
  67. // = | < | > | <> | <= | >= | => | =<
  68. | OpB Eq | OpB Lt | OpB Gt | OpB Ne | OpB Le | OpB Ge | OpB Eg | OpB El-> 4
  69. | OpB Plus | OpB Minus -> 5
  70. | OpB Mod -> 6
  71. | OpB Times | OpB Divide -> 7
  72. | PrefB Neg -> 8
  73. type Expr =
  74. | Val of Value
  75. | Var of string
  76. | Func of string * Expr list
  77. | Arr of string * Expr list
  78. | UnarExpr of UnarOp * Expr
  79. | Expr of Op * Expr * Expr
  80. type Assign =
  81. | AssignVar of string
  82. | AssignArr of string * Expr
  83. type Statement =
  84. | Assign of Assign * Expr
  85. | AssingCode of Assign * Statement list
  86. | CallSt of string * Expr list
  87. | StarPl of Expr
  88. | If of Expr * Statement list * Statement list
  89. | Act of Expr list * Statement list
  90. | Sign of string
  91. | Comment of string
  92. type Location = Location of string * Statement list