130 likes | 233 Views
Simple Language (SL). Start version 00. Content. SL, the language AG, attribute grammar system First version of SL compiler written in AG. SL examples. let fac :: Int -> Int = <br> -> if n > 0 then n * fac (n-1) else 1
E N D
Simple Language (SL) Start version 00
Content • SL, the language • AG, attribute grammar system • First version of SL compiler written in AG IPT - SL
SL examples let fac :: Int -> Int = \n -> if n > 0 then n * fac (n-1) else 1 fi in fac 6 ni let fac :: Int -> Int = \n -> let cnt :: Ref Int = new 2 ; res :: Ref Int = new 1 in while ( cnt <= n ) do res := res * cnt ; cnt := cnt + 1 od ; res ni in fac 6 ni IPT - SL
SL features • Expression evaluation • Int, Bool, Array, Product, Data, Ref • Assignment • Let (blocks), Lambda (functions) • If, While, Case • Typechecking IPT - SL
SL first version • Integer constant • Generating: listing + code 55 .sl 55 stdout LDC 55 TRAP 0 HALT .ssm IPT - SL
AG: SL parser • Parser combinators + scanner pRoot = sem_Root_Root <$> pExpr pExpr = (sem_Expr_Intexpr . string2int) <$> pInteger string2int = foldl (\val dig -> (10*val + ord dig -ord '0')) 0 • Semantic functions? IPT - SL
AG: SL abstract syntax • Abstract tree representation DATA Root | Root Expr DATA Expr | Intexpr Int Root Root Expr Intexpr 55 tree node tree node type tree node variant IPT - SL
AG: SL aspects (attributes) • Pretty printed output & stack code Root Root pp synthesized attribute copied Expr Intexpr 55 pp text.show $ int • Described by ATTR Root [ || ppexpr: PP_Doc ] SEM Root | Root LHS . ppexpr = "expr_pp" SEM Expr [ || pp: PP_Doc ] | Intexpr LHS . pp = "text.show $ int" IPT - SL
AG notation inherited inh+synth synthesized Attribute type (Hugs or Node) ATTR Root [ | | ppexpr: PP_Doc ] SEM Root | Root LHS . ppexpr = "expr_pp" SEM Expr [ | | pp: PP_Doc ] | Intexpr LHS . pp = "text.show $ int" Attribute name Attribution (Hugs) text for definition, specific for:Node type, Node variant, Attribute, Direction IPT - SL
AG compilation • Generates Haskell • Generate SL.hs from SL.ag compile "SL" allc • Datastructures, semantics, ... data Expr = Expr_Intexpr Int deriving Show -- semantic domains type T_Expr = (SCode,PP_Doc) -- catas sem_Expr (Expr_Intexpr int) = sem_Expr_Intexpr int -- funcs sem_Expr_Intexpr ::Int -> T_Expr sem_Expr_Intexpr int = ( ((S_LDC int:)), (text.show $ int) ) IPT - SL
Scanner • Transforms character sequences to tokens data TokenType = TkSymbol | TkVarid | TkConid | TkKeyword | TkOp | TkString | TkCharb| TkInteger8 | TkInteger10 | TkInteger16 | TkTextnm | TkTextln | TkError deriving (Eq, Ord) type Linenumber = Int type Filename = String newtype Token = Tok (TokenType, String, String, Linenumber, Filename) IPT - SL
Code Generation • For a Simple Stack Machine SEM Root [ || ppscode: PP_Doc ] | Root LHS . ppscode = "( vlist . map (text.textOfS)" ". expr_scode" ". (S_TRAP 0:)" ". (S_HALT:)" "$ []" ")" SEM Expr [ || scode: SCode ] | Intexpr LHS . scode = "(S_LDC int:)" DATA S | HALT | TRAP Int | LDC Int IPT - SL
Simple Stack Machine • Memory (Code, Stack) + Registers + Stack manipulating instructions IPT - SL