2022-02-09 08:26:58 +08:00
|
|
|
|
module Luau.Syntax.ToString where
|
|
|
|
|
|
2022-02-19 03:09:00 +08:00
|
|
|
|
open import Agda.Builtin.Float using (primShowFloat)
|
|
|
|
|
open import Luau.Syntax using (Block; Stat; Expr; VarDec; FunDec; nil; var; var_∈_; addr; _$_; function_is_end; return; local_←_; _∙_; done; block_is_end; _⟨_⟩; _⟨_⟩∈_; number)
|
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 ++ ")"
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
exprToString′ lb nil =
|
|
|
|
|
"nil"
|
2022-02-10 07:14:29 +08:00
|
|
|
|
exprToString′ lb (addr a) =
|
|
|
|
|
addrToString(a)
|
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-02-12 09:03:26 +08:00
|
|
|
|
"(" ++ b ++ "()" ++ lb ++
|
2022-02-10 07:14:29 +08:00
|
|
|
|
" " ++ (blockToString′ (lb ++ " ") B) ++ lb ++
|
|
|
|
|
"end)()"
|
2022-02-19 03:09:00 +08:00
|
|
|
|
exprToString′ lb (number x) = primShowFloat x
|
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"
|