Skip to content

Instantly share code, notes, and snippets.

@jvranish
Created November 25, 2012 16:06
Show Gist options
  • Save jvranish/4144177 to your computer and use it in GitHub Desktop.
Save jvranish/4144177 to your computer and use it in GitHub Desktop.
An example alternative AST construction using GADTs
class HasStmt a where
class HasDef a where
class HasExpr a where
data Module = Module [ParsedDef]
data ParsedDef = ParsedDef (Definition ParsedStmt) SourcePos
data ParsedStmt = ParsedStmt (Statement ParsedDef ParsedStmt ParsedExpr) SourcePos
data ParsedExpr = ParsedExpr (Expr ParsedExpr) SourcePos
instance HasDef ParsedDef where
instance HasStmt ParsedStmt where
instance HasExpr ParsedExpr where
data Definition stmt where
VariableDef :: String -> Definition stmt
ForeignDef :: String -> String -> Definition stmt
FunctionDef :: (HasStmt stmt) => String -> [Definition stmt] -> stmt -> Definition stmt
data Statement def stmt expr where
Assign :: (HasExpr expr) => String -> expr -> Statement def stmt expr
If :: (HasExpr expr, HasStmt stmt) => expr -> stmt -> Maybe stmt -> Statement def stmt expr
While :: (HasExpr expr, HasStmt stmt) => expr -> stmt -> Statement def stmt expr
CallStmt :: (HasExpr expr) => expr -> Statement def stmt expr
Return :: (HasExpr expr) => expr -> Statement def stmt expr
Block :: (HasExpr expr, HasDef def, HasStmt stmt) => ([def], [stmt]) -> Statement def stmt expr
data Expr expr where
Call :: (HasExpr expr) => expr -> [expr] -> Expr expr
Identifier :: String -> Expr expr
Literal :: Literal -> Expr expr
data Literal = Lit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment