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

SCM Repository

[diderot] View of /branches/pure-cfg/src/compiler/parser/diderot.grm
ViewVC logotype

View of /branches/pure-cfg/src/compiler/parser/diderot.grm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 824 - (download) (annotate)
Thu Apr 14 15:54:24 2011 UTC (8 years, 4 months ago) by jhr
File size: 9797 byte(s)
  Some debugging and added "^" as power operator.
(* diderot.grm
 *
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *)

%name Diderot;

%tokens
  : KW_strand		("strand")
  | KW_bool		("bool")
  | KW_D		("D")		(* either D *)
  | KW_die		("die")
  | KW_else		("else")
  | KW_field		("field")
  | 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_new		("new")
  | KW_output		("output")
  | KW_real		("real")
  | KW_stabilize	("stabilize")
  | KW_string		("string")
  | KW_tensor		("tensor")
  | KW_update		("update")
  | KW_vec2		("vec2")
  | KW_vec3		("vec3")
  | KW_vec4		("vec4")
  | KW_zeros		("zeros")	(* zero tensor *)
  | OP_eq		("=")
  | 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_exp		("^")
  | OP_at		("@")
  | OP_D		("∇")
  | OP_Dotimes		("∇⊗")
  | OP_Dtimes		("∇×")
  | OP_Ddot		("∇•")
  | LP			("(")
  | RP			(")")
  | LB			("[")
  | RB			("]")
  | LCB			("{")
  | RCB			("}")
  | COMMA		(",")
  | SEMI		(";")
  | COLON		(":")
  | HASH		("#")
  | BANG		("!")
  | BAR			("|")
  | DOTDOT		("..")
  | INT of IntInf.int
  | FLOAT of FloatLit.float
  | STRING of string
  | ID of Atom.atom
  ;

%defs (
  structure PT = ParseTree
  structure L = Literal
  structure B = BasisNames

(* 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
	: Decl+		=> (PT.Program{span=FULL_SPAN, tree=Decl})
	;

Decl
	: InputDecl
	| VarDecl	=> (PT.D_Var VarDecl)
	| StrandDecl
	| CoordinationDecl
	;


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

InputDecl
	: "input" ValueType ID ( "=" Expr )? ";"
		=> (markDecl(FULL_SPAN, PT.D_Input(ValueType, ID, SR)))
	;

(***** variable declarations *****)

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


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

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

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

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

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
	: "update"	=> (Atom.atom "update")
	| "stabilize"	=> (Atom.atom "stabilize")
	| ID		=> (ID)
	;

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

Stmt
	: AtomicStmt
		=> (AtomicStmt)
	| %try "if" "(" Expr ")" Stmt "else" Stmt
		=> (markStmt(FULL_SPAN, PT.S_IfThenElse(Expr, Stmt1, Stmt2)))
	| %try "if" "(" Expr ")" Stmt
		=> (markStmt(FULL_SPAN, PT.S_IfThen(Expr, Stmt)))
	;

AtomicStmt
	: Block
		=> (Block)
	| VarDecl
		=> (PT.S_Decl VarDecl)
	| "stabilize" ";"
		=> (markStmt(FULL_SPAN, PT.S_Stabilize))
	| "die" ";"
		=> (markStmt(FULL_SPAN, PT.S_Die))
	| "new" ID "(" Arguments ")" ";"
		=> (markStmt(FULL_SPAN, PT.S_New(ID, Arguments)))
	| ID "=" Expr ";"
		=> (markStmt(FULL_SPAN, PT.S_Assign(ID, Expr)))
	;

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


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

CoordinationDecl
	: "initially" (Array | Collection) ";"
		=> (markDecl(FULL_SPAN, SR))
	;

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))
	| ValueType ("{" Dimension "}")*
		=> (case SR
		     of [] => ValueType
		      | d => markTy(FULL_SPAN, List.foldl (fn (dim, ty) => PT.T_Seq(ty, dim)) ValueType d)
		    (* end case *))
	;

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))
	;

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
	: "<" => (B.op_lt) | "<=" => (B.op_lte)
	| "==" => (B.op_equ) | "!=" => (B.op_neq)
	| ">=" => (B.op_gte) | ">" => (B.op_gt)
	;

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

AddOp
	: "+" => (B.op_add) | "-" => (B.op_sub)
	;

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

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

PowerExpr
	: ProbeExpr ( "^" ProbeExpr => (B.op_exp, ProbeExpr) )*
		=> (mkLBinExp (ProbeExpr, SR))
	;

ProbeExpr
	: PrefixExpr ( "@" AtomExpr => (AtomExpr) )?
		=> (case SR
		     of NONE => PrefixExpr
		      | SOME e => markExpr(FULL_SPAN, PT.E_BinOp(PrefixExpr, B.op_at, e))
		    (* end case *))
	;

PrefixExpr
	: SliceExpr
		=> (SliceExpr)
	| "-" PrefixExpr
		=> (markExpr(FULL_SPAN, PT.E_UnaryOp(B.op_neg, PrefixExpr)))
	| "!" PrefixExpr
		=> (markExpr(FULL_SPAN, PT.E_UnaryOp(B.op_not, PrefixExpr)))
	| "D" PrefixExpr
		=> (markExpr(FULL_SPAN, PT.E_Apply(B.fn_D, [PrefixExpr])))
	;

SliceExpr
	: DerivExpr Suffix*
		=> (case Suffix
		     of [] => DerivExpr
		      | ss => markExpr(FULL_SPAN, List.foldl (fn (f, e) => f e) DerivExpr ss)
		    (* end case *))
	;

Suffix
	: "[" Indices "]"
		=> (fn e => PT.E_Slice(e, Indices))
	| "{" Expr "}"
		=> (fn e => PT.E_Subscript(e, Expr))
	;

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

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

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

AtomExpr
	: ID ( "(" Arguments ")" )?
		=> (case SR
		     of NONE => markExpr(FULL_SPAN, PT.E_Var ID)
		      | SOME args => markExpr(FULL_SPAN, PT.E_Apply(ID, args))
		    (* end case *))
	| KW_identity "[" Dimension "]"
		=> (markExpr(FULL_SPAN, PT.E_Id Dimension))
	| KW_zeros Dimensions
		=> (markExpr(FULL_SPAN, PT.E_Zero Dimensions))
	| "real" "(" Expr ")"
		=> (markExpr(FULL_SPAN, PT.E_Real Expr))
	| "(" Expr ("," Expr)* ")"
		=> (case SR
		     of [] => Expr
		      | l => markExpr(FULL_SPAN, PT.E_Tuple(Expr::l))
		    (* end case *))
	| "{" Expr ("," Expr)* "}"
		=> (markExpr(FULL_SPAN, PT.E_Sequence(Expr::SR)))
	| "[" Expr ("," Expr)* "]"
		=> (markExpr(FULL_SPAN, PT.E_Cons(Expr::SR)))
	| INT
		=> (markExpr(FULL_SPAN, PT.E_Lit(L.Int INT)))
	| FLOAT
		=> (markExpr(FULL_SPAN, PT.E_Lit(L.Float FLOAT)))
	| STRING
		=> (markExpr(FULL_SPAN, PT.E_Lit(L.String STRING)))
	| "|" Expr "|"
		=> (markExpr(FULL_SPAN, PT.E_UnaryOp(B.op_norm, Expr)))
	;

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