-- This Happy file was machine-generated by the BNF converter
{
{-# OPTIONS_GHC -fno-warn-incomplete-patterns -fno-warn-overlapping-patterns #-}
module ParSRL where
import qualified AbsSRL
import LexSRL
}

%name pSRL SRL
%name pListBlk ListBlk
%name pBlk Blk
%name pStep Step
%name pExp Exp
%name pExp2 Exp2
%name pExp3 Exp3
%name pExp4 Exp4
%name pExp5 Exp5
%name pExp6 Exp6
%name pExp7 Exp7
%name pExp8 Exp8
%name pExp9 Exp9
%name pExp1 Exp1
-- no lexer declaration
%monad { Either String } { (>>=) } { return }
%tokentype {Token}
%token
  '!=' { PT _ (TS _ 1) }
  '(' { PT _ (TS _ 2) }
  ')' { PT _ (TS _ 3) }
  '*' { PT _ (TS _ 4) }
  '+' { PT _ (TS _ 5) }
  '++' { PT _ (TS _ 6) }
  '+=' { PT _ (TS _ 7) }
  '-' { PT _ (TS _ 8) }
  '--' { PT _ (TS _ 9) }
  '-=' { PT _ (TS _ 10) }
  '/' { PT _ (TS _ 11) }
  '<' { PT _ (TS _ 12) }
  '<=' { PT _ (TS _ 13) }
  '=' { PT _ (TS _ 14) }
  '>' { PT _ (TS _ 15) }
  '>=' { PT _ (TS _ 16) }
  '[' { PT _ (TS _ 17) }
  ']' { PT _ (TS _ 18) }
  '^' { PT _ (TS _ 19) }
  '^=' { PT _ (TS _ 20) }
  'do' { PT _ (TS _ 21) }
  'else' { PT _ (TS _ 22) }
  'empty' { PT _ (TS _ 23) }
  'fi' { PT _ (TS _ 24) }
  'for' { PT _ (TS _ 25) }
  'from' { PT _ (TS _ 26) }
  'ftimes' { PT _ (TS _ 27) }
  'if' { PT _ (TS _ 28) }
  'loop' { PT _ (TS _ 29) }
  'pop' { PT _ (TS _ 30) }
  'push' { PT _ (TS _ 31) }
  'skip' { PT _ (TS _ 32) }
  'then' { PT _ (TS _ 33) }
  'top' { PT _ (TS _ 34) }
  'ttimes' { PT _ (TS _ 35) }
  'until' { PT _ (TS _ 36) }
  L_Ident  { PT _ (TV $$) }
  L_integ  { PT _ (TI $$) }

%%

Ident :: { AbsSRL.Ident}
Ident  : L_Ident { AbsSRL.Ident $1 }

Integer :: { Integer }
Integer  : L_integ  { (read ($1)) :: Integer }

SRL :: { AbsSRL.SRL }
SRL : ListBlk { AbsSRL.ESRL $1 }

ListBlk :: { [AbsSRL.Blk] }
ListBlk : Blk { (:[]) $1 } | Blk ListBlk { (:) $1 $2 }

Blk :: { AbsSRL.Blk }
Blk : Step { AbsSRL.Bstep $1 }
    | 'if' Exp 'then' ListBlk 'else' ListBlk 'fi' Exp { AbsSRL.Bif $2 $4 $6 $8 }
    | 'from' Exp 'do' ListBlk 'loop' ListBlk 'until' Exp { AbsSRL.Bfrom $2 $4 $6 $8 }
    | 'for' Exp 'ttimes' ListBlk 'do' ListBlk 'loop' ListBlk 'ftimes' ListBlk 'until' Exp { AbsSRL.Bfor $2 $4 $6 $8 $10 $12 }

Step :: { AbsSRL.Step }
Step : Ident '+=' Exp { AbsSRL.SasnAdd $1 $3 }
     | Ident '-=' Exp { AbsSRL.SasnMin $1 $3 }
     | Ident '^=' Exp { AbsSRL.SasnXor $1 $3 }
     | Ident '[' Exp ']' '+=' Exp { AbsSRL.SasnArrAdd $1 $3 $6 }
     | Ident '[' Exp ']' '-=' Exp { AbsSRL.SasnArrMin $1 $3 $6 }
     | Ident '[' Exp ']' '^=' Exp { AbsSRL.SasnArrXor $1 $3 $6 }
     | Ident '++' { AbsSRL.Sinc $1 }
     | Ident '--' { AbsSRL.Sdec $1 }
     | 'push' Ident Ident { AbsSRL.Spush $2 $3 }
     | 'pop' Ident Ident { AbsSRL.Spop $2 $3 }
     | 'skip' { AbsSRL.Sskip }

Exp :: { AbsSRL.Exp }
Exp : 'top' Exp2 { AbsSRL.Etop $2 } | Exp1 { $1 }

Exp2 :: { AbsSRL.Exp }
Exp2 : 'empty' Exp3 { AbsSRL.Eempty $2 } | Exp3 { $1 }

Exp3 :: { AbsSRL.Exp }
Exp3 : Exp3 '^' Exp4 { AbsSRL.Exor $1 $3 } | Exp4 { $1 }

Exp4 :: { AbsSRL.Exp }
Exp4 : Exp4 '=' Exp5 { AbsSRL.Eeq $1 $3 }
     | Exp4 '!=' Exp5 { AbsSRL.Ene $1 $3 }
     | Exp5 { $1 }

Exp5 :: { AbsSRL.Exp }
Exp5 : Exp5 '<' Exp6 { AbsSRL.Elt $1 $3 }
     | Exp5 '>' Exp6 { AbsSRL.Egt $1 $3 }
     | Exp5 '>=' Exp6 { AbsSRL.Ele $1 $3 }
     | Exp5 '<=' Exp6 { AbsSRL.Ege $1 $3 }
     | Exp6 { $1 }

Exp6 :: { AbsSRL.Exp }
Exp6 : Exp6 '+' Exp7 { AbsSRL.Eadd $1 $3 }
     | Exp6 '-' Exp7 { AbsSRL.Emin $1 $3 }
     | Exp7 { $1 }

Exp7 :: { AbsSRL.Exp }
Exp7 : Exp7 '*' Exp8 { AbsSRL.Emul $1 $3 }
     | Exp7 '/' Exp8 { AbsSRL.Ediv $1 $3 }
     | Exp8 { $1 }

Exp8 :: { AbsSRL.Exp }
Exp8 : Exp9 '[' Exp ']' { AbsSRL.Earr $1 $3 } | Exp9 { $1 }

Exp9 :: { AbsSRL.Exp }
Exp9 : Integer { AbsSRL.Econ $1 }
     | Ident { AbsSRL.Evar $1 }
     | '(' Exp ')' { $2 }

Exp1 :: { AbsSRL.Exp }
Exp1 : Exp2 { $1 }
{

happyError :: [Token] -> Either String a
happyError ts = Left $
  "syntax error at " ++ tokenPos ts ++
  case ts of
    []      -> []
    [Err _] -> " due to lexer error"
    t:_     -> " before `" ++ (prToken t) ++ "'"

myLexer = tokens
}

