Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/vis15/src/compiler/parser/diderot.grm
ViewVC logotype

View of /branches/vis15/src/compiler/parser/diderot.grm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3382 - (download) (annotate)
Sat Nov 7 03:51:29 2015 UTC (4 years, 8 months ago) by jhr
File size: 18843 byte(s)
working on merge
(* diderot.grm
 *
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2015 The University of Chicago
 * All rights reserved.
 *)

%name Diderot;

%tokens
  : KW_bool             ("bool")
  | KW_continue         ("continue")
  | KW_die              ("die")
  | KW_else             ("else")
  | KW_false            ("false")
  | KW_field            ("field")
  | KW_foreach          ("foreach")
  | KW_function         ("function")
  | KW_identity         ("identity")    (* identity matrix *)
  | KW_if               ("if")
  | KW_image            ("image")
  | KW_input            ("input")
  | KW_in               ("in")
  | KW_initially        ("initially")
  | KW_int              ("int")
  | KW_kernel           ("kernel")
  | KW_load             ("load")
  | KW_nan              ("nan")
  | KW_new              ("new")
  | KW_output           ("output")
  | KW_print            ("print")
  | KW_real             ("real")
  | KW_return           ("return")
  | KW_stabilize        ("stabilize")
  | KW_strand           ("strand")
  | KW_string           ("string")
  | KW_tensor           ("tensor")
  | KW_true             ("true")
  | KW_update           ("update")
  | KW_vec2             ("vec2")
  | KW_vec3             ("vec3")
  | KW_vec4             ("vec4")
  | KW_zeros            ("zeros")       (* zero tensor *)
  | OP_eq               ("=")
  | OP_pluseq           ("+=")
  | OP_minuseq          ("-=")
  | OP_stareq           ("*=")
  | OP_slasheq          ("/=")
  | OP_modeq            ("%=")
  | OP_orelse           ("||")
  | OP_andalso          ("&&")
  | OP_lt               ("<")
  | OP_lte              ("<=")
  | OP_eqeq             ("==")
  | OP_neq              ("!=")
  | OP_gte              (">=")
  | OP_gt               (">")
  | OP_plus             ("+")
  | OP_minus            ("-")
  | OP_star             ("*")
  | OP_convolve         ("⊛")
  | OP_dot              ("•")
  | OP_cross            ("×")
  | OP_outer            ("⊗")
  | OP_slash            ("/")
  | OP_mod              ("%")
  | OP_exp              ("^")
  | OP_at               ("@")
  | OP_D                ("∇")
  | OP_Dotimes          ("∇⊗")
  | OP_curl             ("∇×")
  | OP_Ddot             ("∇•")
  | LP                  ("(")
  | RP                  (")")
  | LB                  ("[")
  | RB                  ("]")
  | LCB                 ("{")
  | RCB                 ("}")
  | COMMA               (",")
  | SEMI                (";")
  | COLON               (":")
  | HASH                ("#")
  | BANG                ("!")
  | BAR                 ("|")
  | DOT                 (".")
  | DOTDOT              ("..")
  | INT of IntLit.t
  | REAL of RealLit.t
  | STRING of string
  | ID of Atom.atom
  ;

%keywords
  KW_bool, KW_continue, KW_die, KW_else, KW_field, KW_foreach, KW_function, KW_identity,
  KW_if, KW_image, KW_initially, KW_int, KW_kernel, KW_load, KW_new, KW_output, KW_print,
  KW_real, KW_return, KW_stabilize, KW_strand, KW_string, KW_tensor, KW_update, KW_vec2,
  KW_vec3, KW_vec4, KW_zeros;

%defs (
  structure PT = ParseTree
  structure L = Literal
  structure Op = Operators

(* apply a mark constructor to a span and a tree *)
  fun mark cons (span : AntlrStreamPos.span, tr) = cons{span = span, tree = tr}

(* specialize mark functions for common node types *)
  val markDecl = mark PT.D_Mark
  fun markTy (_, e as PT.T_Mark _) = e
    | markTy (sp, tr) = mark PT.T_Mark (sp, tr)
  fun markStmt (_, e as PT.S_Mark _) = e
    | markStmt (sp, tr) = mark PT.S_Mark (sp, tr)
  fun markExpr (_, e as PT.E_Mark _) = e
    | markExpr (sp, tr) = mark PT.E_Mark (sp, tr)

  fun mkCondExp cons = let
        fun mk (e, []) = e
          | mk (e, [e']) = cons(e, e')
          | mk (e, e'::r) = cons(e, mk(e', r))
        in
          mk
        end

(* build an application for an infix binary operator *)
  fun mkBinApp (e1, rator, e2) = PT.E_BinOp(e1, rator, e2)

(* construct application expressions for left-associative binary operators *)
  fun mkLBinExp (e, []) = e
    | mkLBinExp (e, (id, e')::r) = mkLBinExp (mkBinApp(e, id, e'), r)

(* construct application expressions for right-associative binary operators *)
  fun mkRBinExp (e, []) = e
    | mkRBinExp (e, [(id, e')]) = mkBinApp(e, id, e')
    | mkRBinExp (e, (id, e')::r) = mkBinApp(e, id, mkRBinExp(e', r))

  fun mkOptExp (_, e, NONE) = e
    | mkOptExp (spn, e, SOME mk) = mk(spn, e)

  fun flatten NONE = []
    | flatten (SOME(x, xs)) = x::xs

);

File
        : GlobalDecl* StrandDecl CoordinationDecl
                => (PT.Program{span=FULL_SPAN, tree=GlobalDecl @ [StrandDecl, CoordinationDecl]})
        ;

GlobalDecl
        : InputDecl
        | VarDecl
                => (mark PT.D_Mark (FULL_SPAN, PT.D_Var VarDecl))
        | FunDecl
        ;


(***** Inputs *****)

InputDecl
        : "input" InputType ID ("(" STRING ")")? ( "=" Initializer )? ";"
                => (markDecl(FULL_SPAN, PT.D_Input(InputType, ID, SR1, SR2)))
        ;


(***** Variable declarations *****)

VarIterationDecl 
        : ValueType ID "in" Expr 
                => (mark PT.VD_Mark (FULL_SPAN, PT.VD_Decl(PT.T_DynSeq(ValueType), ID, Expr))) 
        ;

VarDecl
        : Type ID "=" Expr ";"
                => (mark PT.VD_Mark (FULL_SPAN, PT.VD_Decl(Type, ID, Expr)))
        ;


(***** Function declarations *****)

FunDecl
        : "function" ConcreteType ID "(" Params ")" FunBody
                => (markDecl(FULL_SPAN, PT.D_Func(ConcreteType, ID, Params, FunBody)))
        ;

Params
        : ( Param ( "," Param )* )?
                => (flatten SR)
        ;

Param
        : ValueType ID
                => (mark PT.P_Mark (FULL_SPAN, PT.P_Param(ValueType, ID)))
        ;

FunBody
        : "=" Expr ";"
                => (PT.FB_Expr Expr)
        | Block
                => (PT.FB_Stmt Block)
        ;


(***** Strands *****)

StrandDecl
        : "strand" ID "(" Params ")" "{" StrandStateDecl* StrandMethod+ "}"
                => (markDecl(FULL_SPAN, PT.D_Strand{
                      name = ID, params = Params, state = StrandStateDecl,
                      methods = StrandMethod
                    }))
        ;

StrandStateDecl
        : "output" VarDecl
                => (true, mark PT.VD_Mark (FULL_SPAN, VarDecl))
        | VarDecl
                => (false, VarDecl)
        ;

StrandMethod
        : MethodId Block
                => (mark PT.M_Mark (FULL_SPAN, PT.M_Method(MethodId, Block)))
        ;

MethodId
        : "initially"   => (StrandUtil.Initially)
        | "update"      => (StrandUtil.Update)
        | "stabilize"   => (StrandUtil.Stabilize)
        ;

Block
        : "{" Stmt* "}"
                => (markStmt(FULL_SPAN, PT.S_Block Stmt))
        ;

Stmt
        : Block
                => (Block)
        | "foreach" "(" VarIterationDecl ")" Block 
                => (markStmt(FULL_SPAN, PT.S_Foreach(VarIterationDecl, Block)))
        | "if" "(" Expr ")" Block ("else" Block)?
                => (case SR
		     of SOME stm => markStmt(span, PT.S_IfThenElse(Expr, Block, stm)
		      | NONE => markStmt(span, PT.S_IfThen(Expr, Block)))
		    (* end case *))
        | VarDecl
                => (PT.S_Decl VarDecl)
        | "stabilize" ";"
                => (markStmt(FULL_SPAN, PT.S_Stabilize))
        | "continue" ";"
                => (markStmt(FULL_SPAN, PT.S_Continue))
        | "die" ";"
                => (markStmt(FULL_SPAN, PT.S_Die))
        | "new" ID "(" Arguments ")" ";"
                => (markStmt(FULL_SPAN, PT.S_New(ID, Arguments)))
        | "print" "(" Expr ("," Expr)* ")" ";"
                => (markStmt(FULL_SPAN, PT.S_Print(Expr::SR)))
        | ID "=" Expr ";"
                => (markStmt(FULL_SPAN, PT.S_Assign(ID, Expr)))
        | ID "+=" Expr ";"
                => (markStmt(FULL_SPAN, PT.S_OpAssign(ID, Op.asgn_add, Expr)))
        | ID "-=" Expr ";"
                => (markStmt(FULL_SPAN, PT.S_OpAssign(ID, Op.asgn_sub, Expr)))
        | ID "*=" Expr ";"
                => (markStmt(FULL_SPAN, PT.S_OpAssign(ID, Op.asgn_mul, Expr)))
        | ID "/=" Expr ";"
                => (markStmt(FULL_SPAN, PT.S_OpAssign(ID, Op.asgn_div, Expr)))
        | ID "%=" Expr ";"
                => (markStmt(FULL_SPAN, PT.S_OpAssign(ID, Op.asgn_mod, Expr)))
        | "return" Expr ";"
                => (markStmt(FULL_SPAN, PT.S_Return Expr))
        ;

Arguments
        : ( Expr ("," Expr)* )?
                => (flatten SR)
        ;


(***** Coordination declaration *****)

CoordinationDecl
        : "initially" (Array | Collection) ";"
                => (markDecl(FULL_SPAN, SR))
        | "update" Block 
                => (markDecl(FULL_SPAN, PT.D_Update Block))
        ;

Array
        : "[" Create "|" Iterations "]"
                => (PT.D_InitialArray(Create, Iterations))
        ;

Create
        : ID "(" Arguments ")"
                => (mark PT.C_Mark (FULL_SPAN, PT.C_Create(ID, Arguments)))
        ;

Iterations
        : Iteration ( "," Iteration )*
                => (Iteration :: SR)
        ;

Iteration
        : ID "in" Expr ".." Expr
                => (mark PT.I_Mark (FULL_SPAN, PT.I_Range(ID, Expr1, Expr2)))
        ;

Collection
        : "{" Create  "|" Iterations "}"
                => (PT.D_InitialCollection(Create, Iterations))
        ;


(***** Types *****)

Type    : "image" "(" Dimension ")" Dimensions
                => (markTy(FULL_SPAN, PT.T_Image{
                        shape = Dimensions, dim = Dimension
                      }))
        | "field" "#" Dimension "(" Dimension ")" Dimensions
                => (markTy(FULL_SPAN, PT.T_Field{
                        diff = Dimension1,
                        shape = Dimensions,
                        dim = Dimension2
                      }))
        | "kernel" "#" Dimension
                => (markTy(FULL_SPAN, PT.T_Kernel Dimension))
        | ConcreteType
        ;

ConcreteType
        : ValueType SeqDimensions
                => (markTy(FULL_SPAN, SeqDimensions ValueType))
        ;

SeqDimensions
        : (* empty *)
                => (fn ty => ty)
        | "{" "}"
                => (fn ty => PT.T_DynSeq ty)
        | "{" Dimension "}" SeqDimensions
                => (fn ty => SeqDimensions(PT.T_Seq(ty, Dimension)))
        ;

InputType
        : "image" "(" Dimension ")" Dimensions
                => (markTy(FULL_SPAN, PT.T_Image{
                        shape = Dimensions, dim = Dimension
                      }))
        | ValueType SeqDimensions
                => (markTy(FULL_SPAN, SeqDimensions ValueType))
        ;

ValueType
        : "tensor" Dimensions
                => (markTy(FULL_SPAN, PT.T_Tensor Dimensions))
        | "vec2"
                => (markTy(FULL_SPAN, PT.T_Vec 2))
        | "vec3"
                => (markTy(FULL_SPAN, PT.T_Vec 3))
        | "vec4"
                => (markTy(FULL_SPAN, PT.T_Vec 4))
        | "bool"
                => (markTy(FULL_SPAN, PT.T_Bool))
        | "int"
                => (markTy(FULL_SPAN, PT.T_Int))
        | "real"
                => (markTy(FULL_SPAN, PT.T_Real))
        | "string"
                => (markTy(FULL_SPAN, PT.T_String))
        | ID
                => (markTy(FULL_SPAN, PT.T_Strand ID))
        ;

Dimensions
        : "[" ( Dimension ("," Dimension)* )? "]"
                => (flatten SR)
        ;

Dimension
        : INT
                => (INT)
        ;

(***** Expressions *****)

Expr
        : TestExpr ( "if" Expr "else" Expr => (Expr1, Expr2) )?
                => (case SR
                     of NONE => TestExpr
                      | SOME(e1, e2) => markExpr(FULL_SPAN, PT.E_Cond(TestExpr, e1, e2))
                    (* end case *))
        ;

TestExpr
        : AndExpr ( "||" AndExpr )*
                => (mkCondExp PT.E_OrElse (AndExpr, SR))
        ;

AndExpr
        : CmpExpr ( "&&" CmpExpr )*
                => (mkCondExp PT.E_AndAlso (CmpExpr, SR))
        ;

CmpExpr
        : AddExpr ( CmpOp AddExpr )*
                => (mkLBinExp (AddExpr, SR))
        ;

CmpOp
        : "<" => (Op.op_lt) | "<=" => (Op.op_lte)
        | "==" => (Op.op_equ) | "!=" => (Op.op_neq)
        | ">=" => (Op.op_gte) | ">" => (Op.op_gt)
        ;

AddExpr
        : MulExpr ( AddOp MulExpr )*
                => (mkLBinExp (MulExpr, SR))
        ;

AddOp
        : "+" => (Op.op_add) | "-" => (Op.op_sub) | "@" => (Op.op_at)
        ;

MulExpr
        : PrefixExpr ( MulOp PrefixExpr )*
                => (mkLBinExp (PrefixExpr, SR))
        ;

MulOp
        : "*" => (Op.op_mul) | "/" => (Op.op_div) | "%" => (Op.op_mod)
	| "⊛" => (Op.op_convolve) | "•" => (Op.op_dot) | "×" => (Op.op_cross)
	| "⊗" => (Op.op_outer) | ":" => (Op.op_colon)
        ;

PrefixExpr
        : PowerExpr
                => (PowerExpr)
        | "-" PrefixExpr
                => (markExpr(FULL_SPAN, PT.E_UnaryOp(Op.op_neg, PrefixExpr)))
        | "!" PrefixExpr
                => (markExpr(FULL_SPAN, PT.E_UnaryOp(Op.op_not, PrefixExpr)))
        ;

PowerExpr
        : SuffixExpr ( "^" SuffixExpr => (Op.op_exp, SuffixExpr) )*
                => (mkRBinExp (SuffixExpr, SR))
        ;

SuffixExpr
        : DerivExpr Suffix*
                => (case Suffix
                     of [] => DerivExpr
                      | ss => markExpr(FULL_SPAN, List.foldl (fn (f, e) => f e) DerivExpr ss)
                    (* end case *))
        | "identity" "[" Dimension "]"
                => (markExpr(FULL_SPAN, PT.E_Id Dimension))
        | "zeros" Dimensions?
                => (markExpr(FULL_SPAN, PT.E_Zero(getOpt(Dimensions, []))))
        | "real" "(" Expr ")"
                => (markExpr(FULL_SPAN, PT.E_Real Expr))
        | "nan" Dimensions?
                => (markExpr(FULL_SPAN, PT.E_NaN(getOpt(Dimensions, []))))
        ;

Suffix
        : "(" Arguments ")"
                => (fn e => markExpr(FULL_SPAN, PT.E_Apply(e, Arguments)))
        | "[" Indices "]"
                => (fn e => PT.E_Slice(e, Indices))
        |  "." ID 
                => (fn e => PT.E_Select(e, ID))
        | "{" Expr ("|" ID "in" Expr => (ID, Expr))? "}"
                => (case SR 
                     of NONE  => (fn e => PT.E_Subscript(e, Expr))
                      | SOME(x, e') =>
                          (fn e => PT.E_Apply(e, [
                              markExpr(FULL_SPAN, PT.E_SeqComp(Expr, x, e'))
                            ]))  
                    (* end case *)) 
        ;

Indices
        : Index ( "," Index )*
                => (Index :: SR)
        ;

Index
        : ":"
                => (NONE)
        | Expr
                => (SOME Expr)
        ;

DerivExpr
        : AtomExpr
                => (AtomExpr)
        | "∇" AtomExpr
                => (markExpr(FULL_SPAN, PT.E_UnaryOp(Op.op_D, AtomExpr)))
        | "∇⊗" DerivExpr
                => (markExpr(FULL_SPAN, PT.E_UnaryOp(Op.op_Dotimes, DerivExpr)))
        | "∇×" DerivExpr
                => (markExpr(FULL_SPAN, PT.E_UnaryOp(Op.op_curl, DerivExpr)))
        | "∇•" DerivExpr
                => (markExpr(FULL_SPAN, PT.E_UnaryOp(Op.op_Ddot, DerivExpr)))
        ;

AtomExpr
        : ID
                => (markExpr(FULL_SPAN, PT.E_Var ID))
        | LoadExpr
                => (LoadExpr)
        | "(" Expr ("," Expr)* ")"
                => (case SR
                     of [] => Expr
                      | l => markExpr(FULL_SPAN, PT.E_Tuple(Expr::l))
                    (* end case *))
        | "{" "}"
                => (markExpr(FULL_SPAN, PT.E_Sequence[]))
        | "{" Expr SeqRest "}"
                => (SeqRest(FULL_SPAN, Expr))
        | "[" Expr ("," Expr)* "]"
                => (markExpr(FULL_SPAN, PT.E_Cons(Expr::SR)))
        | INT
                => (markExpr(FULL_SPAN, PT.E_Lit(L.Int INT)))
        | REAL
                => (markExpr(FULL_SPAN, PT.E_Lit(L.Real REAL)))
        | STRING
                => (markExpr(FULL_SPAN, PT.E_Lit(L.String STRING)))
        | "true"
                => (markExpr(FULL_SPAN, PT.E_Lit(L.Bool true)))
        | "false"
                => (markExpr(FULL_SPAN, PT.E_Lit(L.Bool false)))
        | "|" Expr "|"
                => (markExpr(FULL_SPAN, PT.E_UnaryOp(Op.op_norm, Expr)))
        ;

SeqRest
        : ("," Expr)*
                => (fn (span, e) => markExpr(span, PT.E_Sequence(e::SR)))
        | "|" ID "in" Expr
                => (fn (span, e) => markExpr(span, PT.E_SeqComp(e, ID, Expr)))
        ;

(***** Initializers (constant expressions) *****)

Initializer
        : ConstExpr
        | LoadExpr
                => (LoadExpr)
        ;

ConstExpr
        : AddConstExpr ( CmpOp AddConstExpr )*
                => (mkLBinExp (AddConstExpr, SR))
        ;

AddConstExpr
        : MulConstExpr ( AddOp MulConstExpr )*
                => (mkLBinExp (MulConstExpr, SR))
        ;

MulConstExpr
        : PrefixConstExpr ( MulOp PrefixConstExpr )*
                => (mkLBinExp (PrefixConstExpr, SR))
        ;

PrefixConstExpr
        : PowerConstExpr
                => (PowerConstExpr)
        | "-" PrefixConstExpr
                => (markExpr(FULL_SPAN, PT.E_UnaryOp(Op.op_neg, PrefixConstExpr)))
        | "!" PrefixConstExpr
                => (markExpr(FULL_SPAN, PT.E_UnaryOp(Op.op_not, PrefixConstExpr)))
        ;

PowerConstExpr
        : AtomConstExpr ( "^" AtomConstExpr => (Op.op_exp, AtomConstExpr) )*
                => (mkRBinExp (AtomConstExpr, SR))
        ;

AtomConstExpr
        : ID "(" ConstArguments ")"
                => (markExpr(FULL_SPAN, PT.E_Apply(markExpr(FULL_SPAN, PT.E_Var ID), ConstArguments)))
        | "identity" "[" Dimension "]"
                => (markExpr(FULL_SPAN, PT.E_Id Dimension))
        | "zeros" Dimensions
                => (markExpr(FULL_SPAN, PT.E_Zero Dimensions))
        | "nan" Dimensions?
                => (markExpr(FULL_SPAN, PT.E_NaN(getOpt(Dimensions, []))))
        | "{" Initializer ("," Initializer)* "}"
                => (markExpr(FULL_SPAN, PT.E_Sequence(Initializer::SR)))
        | "[" Initializer ("," Initializer)* "]"
                => (markExpr(FULL_SPAN, PT.E_Cons(Initializer::SR)))
        | INT
                => (markExpr(FULL_SPAN, PT.E_Lit(L.Int INT)))
        | REAL
                => (markExpr(FULL_SPAN, PT.E_Lit(L.Real REAL)))
        | STRING
                => (markExpr(FULL_SPAN, PT.E_Lit(L.String STRING)))
        | "true"
                => (markExpr(FULL_SPAN, PT.E_Lit(L.Bool true)))
        | "false"
                => (markExpr(FULL_SPAN, PT.E_Lit(L.Bool false)))
        ;

ConstArguments
        : ( ConstExpr ("," ConstExpr)* )?
                => (flatten SR)
        ;

LoadExpr
        : "image" "(" STRING ")"
                => (markExpr(FULL_SPAN, PT.E_Image STRING))
        | "load" "(" STRING ")"
                => (markExpr(FULL_SPAN, PT.E_Load STRING))
        ;

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0