2022-02-09 08:26:58 +08:00
|
|
|
|
module Luau.Syntax.ToString where
|
|
|
|
|
|
2022-03-03 06:02:51 +08:00
|
|
|
|
open import Agda.Builtin.Bool using (true; false)
|
2022-02-19 03:09:00 +08:00
|
|
|
|
open import Agda.Builtin.Float using (primShowFloat)
|
2022-03-03 07:26:58 +08:00
|
|
|
|
open import Agda.Builtin.String using (primShowString)
|
|
|
|
|
open import Luau.Syntax using (Value; Block; Stat; Expr; VarDec; FunDec; nil; bool; val; var; var_∈_; addr; _$_; function_is_end; return; local_←_; _∙_; done; block_is_end; _⟨_⟩; _⟨_⟩∈_; number; BinaryOperator; +; -; *; /; <; >; ==; ~=; <=; >=; ··; binexp; string)
|
2022-02-09 08:26:58 +08:00
|
|
|
|
open import FFI.Data.String using (String; _++_)
|
2022-02-10 07:14:29 +08:00
|
|
|
|
open import Luau.Addr.ToString using (addrToString)
|
2022-02-12 09:03:26 +08:00
|
|
|
|
open import Luau.Type.ToString using (typeToString)
|
2022-02-10 07:14:29 +08:00
|
|
|
|
open import Luau.Var.ToString using (varToString)
|
2022-02-09 08:26:58 +08:00
|
|
|
|
|
2022-02-12 09:03:26 +08:00
|
|
|
|
varDecToString : ∀ {a} → VarDec a → String
|
|
|
|
|
varDecToString (var x) = varToString x
|
|
|
|
|
varDecToString (var x ∈ T) = varToString x ++ " : " ++ typeToString T
|
|
|
|
|
|
|
|
|
|
funDecToString : ∀ {a} → FunDec a → String
|
|
|
|
|
funDecToString ("" ⟨ x ⟩∈ T) = "function(" ++ varDecToString x ++ "): " ++ typeToString T
|
|
|
|
|
funDecToString ("" ⟨ x ⟩) = "function(" ++ varDecToString x ++ ")"
|
|
|
|
|
funDecToString (f ⟨ x ⟩∈ T) = "function " ++ varToString f ++ "(" ++ varDecToString x ++ "): " ++ typeToString T
|
|
|
|
|
funDecToString (f ⟨ x ⟩) = "function " ++ varToString f ++ "(" ++ varDecToString x ++ ")"
|
|
|
|
|
|
2022-02-23 07:52:56 +08:00
|
|
|
|
binOpToString : BinaryOperator → String
|
|
|
|
|
binOpToString + = "+"
|
|
|
|
|
binOpToString - = "-"
|
|
|
|
|
binOpToString * = "*"
|
|
|
|
|
binOpToString / = "/"
|
2022-02-25 03:17:46 +08:00
|
|
|
|
binOpToString < = "<"
|
|
|
|
|
binOpToString > = ">"
|
2022-03-03 06:02:51 +08:00
|
|
|
|
binOpToString == = "=="
|
|
|
|
|
binOpToString ~= = "~="
|
|
|
|
|
binOpToString <= = "<="
|
|
|
|
|
binOpToString >= = ">="
|
2022-03-03 07:26:58 +08:00
|
|
|
|
binOpToString ·· = ".."
|
2022-03-03 06:02:51 +08:00
|
|
|
|
|
|
|
|
|
valueToString : Value → String
|
|
|
|
|
valueToString nil = "nil"
|
|
|
|
|
valueToString (addr a) = addrToString a
|
|
|
|
|
valueToString (number x) = primShowFloat x
|
|
|
|
|
valueToString (bool false) = "false"
|
|
|
|
|
valueToString (bool true) = "true"
|
2022-03-03 07:26:58 +08:00
|
|
|
|
valueToString (string x) = primShowString x
|
2022-02-23 07:52:56 +08:00
|
|
|
|
|
2022-02-12 09:03:26 +08:00
|
|
|
|
exprToString′ : ∀ {a} → String → Expr a → String
|
|
|
|
|
statToString′ : ∀ {a} → String → Stat a → String
|
|
|
|
|
blockToString′ : ∀ {a} → String → Block a → String
|
2022-02-09 08:26:58 +08:00
|
|
|
|
|
2022-03-03 06:02:51 +08:00
|
|
|
|
exprToString′ lb (val v) =
|
|
|
|
|
valueToString(v)
|
2022-02-09 08:26:58 +08:00
|
|
|
|
exprToString′ lb (var x) =
|
2022-02-10 07:14:29 +08:00
|
|
|
|
varToString(x)
|
2022-02-09 08:26:58 +08:00
|
|
|
|
exprToString′ lb (M $ N) =
|
|
|
|
|
(exprToString′ lb M) ++ "(" ++ (exprToString′ lb N) ++ ")"
|
2022-02-12 09:03:26 +08:00
|
|
|
|
exprToString′ lb (function F is B end) =
|
|
|
|
|
funDecToString F ++ lb ++
|
2022-02-09 08:26:58 +08:00
|
|
|
|
" " ++ (blockToString′ (lb ++ " ") B) ++ lb ++
|
|
|
|
|
"end"
|
2022-02-10 07:14:29 +08:00
|
|
|
|
exprToString′ lb (block b is B end) =
|
2022-03-03 06:02:51 +08:00
|
|
|
|
"(" ++ varDecToString b ++ "()" ++ lb ++
|
2022-02-10 07:14:29 +08:00
|
|
|
|
" " ++ (blockToString′ (lb ++ " ") B) ++ lb ++
|
|
|
|
|
"end)()"
|
2022-02-23 07:52:56 +08:00
|
|
|
|
exprToString′ lb (binexp x op y) = exprToString′ lb x ++ " " ++ binOpToString op ++ " " ++ exprToString′ lb y
|
2022-02-09 08:26:58 +08:00
|
|
|
|
|
2022-02-12 09:03:26 +08:00
|
|
|
|
statToString′ lb (function F is B end) =
|
|
|
|
|
"local " ++ funDecToString F ++ lb ++
|
2022-02-09 08:26:58 +08:00
|
|
|
|
" " ++ (blockToString′ (lb ++ " ") B) ++ lb ++
|
|
|
|
|
"end"
|
|
|
|
|
statToString′ lb (local x ← M) =
|
2022-02-12 09:03:26 +08:00
|
|
|
|
"local " ++ varDecToString x ++ " = " ++ (exprToString′ lb M)
|
2022-02-09 08:26:58 +08:00
|
|
|
|
statToString′ lb (return M) =
|
|
|
|
|
"return " ++ (exprToString′ lb M)
|
|
|
|
|
|
2022-02-10 07:14:29 +08:00
|
|
|
|
blockToString′ lb (S ∙ done) = statToString′ lb S
|
2022-02-09 08:26:58 +08:00
|
|
|
|
blockToString′ lb (S ∙ B) = statToString′ lb S ++ lb ++ blockToString′ lb B
|
2022-02-10 07:14:29 +08:00
|
|
|
|
blockToString′ lb (done) = ""
|
2022-02-09 08:26:58 +08:00
|
|
|
|
|
2022-02-12 09:03:26 +08:00
|
|
|
|
exprToString : ∀ {a} → Expr a → String
|
2022-02-09 08:26:58 +08:00
|
|
|
|
exprToString = exprToString′ "\n"
|
|
|
|
|
|
2022-02-12 09:03:26 +08:00
|
|
|
|
statToString : ∀ {a} → Stat a → String
|
2022-02-09 08:26:58 +08:00
|
|
|
|
statToString = statToString′ "\n"
|
|
|
|
|
|
2022-02-12 09:03:26 +08:00
|
|
|
|
blockToString : ∀ {a} → Block a → String
|
2022-02-09 08:26:58 +08:00
|
|
|
|
blockToString = blockToString′ "\n"
|