definition module Sil.Syntax

from StdOverloaded import class toString

from Data.Maybe import :: Maybe

from Sil.Types import :: Type

:: Program =
	{ p_funs :: [Function]
	}

:: Function =
	{ f_type :: Type
	, f_name :: Name
	, f_args :: [Arg]
	, f_code :: CodeBlock
	}

:: CodeBlock =
	{ cb_init    :: [Initialisation]
	, cb_content :: [Statement]
	}

:: Arg =
	{ arg_type :: Type
	, arg_name :: Name
	}

:: Initialisation =
	{ init_type :: Type
	, init_name :: Name
	}

:: Statement
	= Declaration Name Expression
	| Application Expression
	| Return (Maybe Expression)
	| If [(Expression, CodeBlock)] (Maybe CodeBlock)
	| While Expression CodeBlock
	| MachineStm String

:: Expression
	= Name Name
	| Literal Literal
	| App Name [Expression]
	| BuiltinApp Op1 Expression
	| BuiltinApp2 Expression Op2 Expression

:: Op1
	= Neg //* ~
	| Not //* !

:: Op2
	= Add    //* +
	| Sub    //* -
	| Mul    //* *
	| Div    //* /
	| Rem    //* %
	| Equals //* ==
	| LogOr  //* ||
	| LogAnd //* &&

:: Literal
	= BLit Bool
	| ILit Int

:: Name :== String

instance toString Statement
instance toString Arg
instance toString Expression
instance toString Op1
instance toString Op2
instance toString Literal

class allStatements a :: a -> [Statement]
instance allStatements Program
instance allStatements Function
instance allStatements CodeBlock
instance allStatements Statement

class allCodeBlocks a :: a -> [CodeBlock]
instance allCodeBlocks Function
instance allCodeBlocks CodeBlock
instance allCodeBlocks Statement

class allLocals a :: a -> [(Type, Name)]
instance allLocals Function
instance allLocals CodeBlock