2022-02-09 00:26:58 +00:00
|
|
|
|
module Luau.Syntax.ToString where
|
|
|
|
|
|
2022-02-18 19:09:00 +00:00
|
|
|
|
open import Agda.Builtin.Float using (primShowFloat)
|
2022-02-22 23:52:56 +00:00
|
|
|
|
open import Luau.Syntax using (Block; Stat; Expr; VarDec; FunDec; nil; var; var_∈_; addr; _$_; function_is_end; return; local_←_; _∙_; done; block_is_end; _⟨_⟩; _⟨_⟩∈_; number; BinaryOperator; +; -; *; /; binexp)
|
2022-02-09 00:26:58 +00:00
|
|
|
|
open import FFI.Data.String using (String; _++_)
|
2022-02-09 23:14:29 +00:00
|
|
|
|
open import Luau.Addr.ToString using (addrToString)
|
2022-02-12 01:03:26 +00:00
|
|
|
|
open import Luau.Type.ToString using (typeToString)
|
2022-02-09 23:14:29 +00:00
|
|
|
|
open import Luau.Var.ToString using (varToString)
|
2022-02-09 00:26:58 +00:00
|
|
|
|
|
2022-02-12 01:03:26 +00: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-22 23:52:56 +00:00
|
|
|
|
binOpToString : BinaryOperator → String
|
|
|
|
|
binOpToString + = "+"
|
|
|
|
|
binOpToString - = "-"
|
|
|
|
|
binOpToString * = "*"
|
|
|
|
|
binOpToString / = "/"
|
|
|
|
|
|
2022-02-12 01:03:26 +00:00
|
|
|
|
exprToString′ : ∀ {a} → String → Expr a → String
|
|
|
|
|
statToString′ : ∀ {a} → String → Stat a → String
|
|
|
|
|
blockToString′ : ∀ {a} → String → Block a → String
|
2022-02-09 00:26:58 +00:00
|
|
|
|
|
|
|
|
|
exprToString′ lb nil =
|
|
|
|
|
"nil"
|
2022-02-09 23:14:29 +00:00
|
|
|
|
exprToString′ lb (addr a) =
|
|
|
|
|
addrToString(a)
|
2022-02-09 00:26:58 +00:00
|
|
|
|
exprToString′ lb (var x) =
|
2022-02-09 23:14:29 +00:00
|
|
|
|
varToString(x)
|
2022-02-09 00:26:58 +00:00
|
|
|
|
exprToString′ lb (M $ N) =
|
|
|
|
|
(exprToString′ lb M) ++ "(" ++ (exprToString′ lb N) ++ ")"
|
2022-02-12 01:03:26 +00:00
|
|
|
|
exprToString′ lb (function F is B end) =
|
|
|
|
|
funDecToString F ++ lb ++
|
2022-02-09 00:26:58 +00:00
|
|
|
|
" " ++ (blockToString′ (lb ++ " ") B) ++ lb ++
|
|
|
|
|
"end"
|
2022-02-09 23:14:29 +00:00
|
|
|
|
exprToString′ lb (block b is B end) =
|
2022-02-12 01:03:26 +00:00
|
|
|
|
"(" ++ b ++ "()" ++ lb ++
|
2022-02-09 23:14:29 +00:00
|
|
|
|
" " ++ (blockToString′ (lb ++ " ") B) ++ lb ++
|
|
|
|
|
"end)()"
|
2022-02-18 19:09:00 +00:00
|
|
|
|
exprToString′ lb (number x) = primShowFloat x
|
2022-02-22 23:52:56 +00:00
|
|
|
|
exprToString′ lb (binexp x op y) = exprToString′ lb x ++ " " ++ binOpToString op ++ " " ++ exprToString′ lb y
|
2022-02-09 00:26:58 +00:00
|
|
|
|
|
2022-02-12 01:03:26 +00:00
|
|
|
|
statToString′ lb (function F is B end) =
|
|
|
|
|
"local " ++ funDecToString F ++ lb ++
|
2022-02-09 00:26:58 +00:00
|
|
|
|
" " ++ (blockToString′ (lb ++ " ") B) ++ lb ++
|
|
|
|
|
"end"
|
|
|
|
|
statToString′ lb (local x ← M) =
|
2022-02-12 01:03:26 +00:00
|
|
|
|
"local " ++ varDecToString x ++ " = " ++ (exprToString′ lb M)
|
2022-02-09 00:26:58 +00:00
|
|
|
|
statToString′ lb (return M) =
|
|
|
|
|
"return " ++ (exprToString′ lb M)
|
|
|
|
|
|
2022-02-09 23:14:29 +00:00
|
|
|
|
blockToString′ lb (S ∙ done) = statToString′ lb S
|
2022-02-09 00:26:58 +00:00
|
|
|
|
blockToString′ lb (S ∙ B) = statToString′ lb S ++ lb ++ blockToString′ lb B
|
2022-02-09 23:14:29 +00:00
|
|
|
|
blockToString′ lb (done) = ""
|
2022-02-09 00:26:58 +00:00
|
|
|
|
|
2022-02-12 01:03:26 +00:00
|
|
|
|
exprToString : ∀ {a} → Expr a → String
|
2022-02-09 00:26:58 +00:00
|
|
|
|
exprToString = exprToString′ "\n"
|
|
|
|
|
|
2022-02-12 01:03:26 +00:00
|
|
|
|
statToString : ∀ {a} → Stat a → String
|
2022-02-09 00:26:58 +00:00
|
|
|
|
statToString = statToString′ "\n"
|
|
|
|
|
|
2022-02-12 01:03:26 +00:00
|
|
|
|
blockToString : ∀ {a} → Block a → String
|
2022-02-09 00:26:58 +00:00
|
|
|
|
blockToString = blockToString′ "\n"
|