first usable version
This commit is contained in:
66
IBAF-cbs/IBAF/IBAF-Start/IBAF-Expressions.cbs
Normal file
66
IBAF-cbs/IBAF/IBAF-Start/IBAF-Expressions.cbs
Normal file
@@ -0,0 +1,66 @@
|
||||
Language "IBAFlang"
|
||||
|
||||
# 1: General expressions
|
||||
Syntax Exp:exp ::= id
|
||||
| int
|
||||
| 'true'
|
||||
| 'false'
|
||||
| exp '+' exp
|
||||
| exp '-' exp
|
||||
| exp '*' exp
|
||||
| exp '/' exp
|
||||
| exp '%' exp
|
||||
| exp '&&' exp
|
||||
| exp '||' exp
|
||||
| exp '==' exp
|
||||
| exp '>=' exp
|
||||
| exp '>' exp
|
||||
| exp '<=' exp
|
||||
| exp '<' exp
|
||||
| '(' exp ')'
|
||||
|
||||
Semantics eval-exp[[ _:exp ]] : => values
|
||||
Rule eval-exp[[ Id ]] = assigned(bound(id[[ Id ]]))
|
||||
Rule eval-exp[[ Int ]] = int-val[[ Int ]]
|
||||
Rule eval-exp[[ 'true' ]] = true
|
||||
Rule eval-exp[[ 'false' ]] = false
|
||||
Rule eval-exp[[ Exp1 '+' Exp2 ]] = int-add(eval-exp[[ Exp1 ]], eval-exp[[ Exp2 ]])
|
||||
Rule eval-exp[[ Exp1 '-' Exp2 ]] = int-sub(eval-exp[[ Exp1 ]], eval-exp[[ Exp2 ]])
|
||||
Rule eval-exp[[ Exp1 '*' Exp2 ]] = int-mul(eval-exp[[ Exp1 ]], eval-exp[[ Exp2 ]])
|
||||
Rule eval-exp[[ Exp1 '/' Exp2 ]] = checked int-div(eval-exp[[ Exp1 ]], eval-exp[[ Exp2 ]])
|
||||
Rule eval-exp[[ Exp1 '%' Exp2 ]] = checked int-mod(eval-exp[[ Exp1 ]], eval-exp[[ Exp2 ]])
|
||||
Rule eval-exp[[ Exp1 '&&' Exp2 ]] = and(eval-exp[[ Exp1 ]], eval-exp[[ Exp2 ]])
|
||||
Rule eval-exp[[ Exp1 '||' Exp2 ]] = and(eval-exp[[ Exp1 ]], eval-exp[[ Exp2 ]])
|
||||
Rule eval-exp[[ Exp1 '==' Exp2 ]] = is-equal(eval-exp[[ Exp1 ]], eval-exp[[ Exp2]] )
|
||||
Rule eval-exp[[ Exp1 '>=' Exp2 ]] = is-greater-or-equal(eval-exp[[ Exp1 ]], eval-exp[[ Exp2]] )
|
||||
Rule eval-exp[[ Exp1 '>' Exp2 ]] = is-greater(eval-exp[[ Exp1 ]], eval-exp[[ Exp2]] )
|
||||
Rule eval-exp[[ Exp1 '<=' Exp2 ]] = is-less-or-equal(eval-exp[[ Exp1 ]], eval-exp[[ Exp2]] )
|
||||
Rule eval-exp[[ Exp1 '<' Exp2 ]] = is-less(eval-exp[[ Exp1 ]], eval-exp[[ Exp2]] )
|
||||
Rule eval-exp[[ '(' Exp ')' ]] = eval-exp[[ Exp ]]
|
||||
|
||||
|
||||
|
||||
Lexis Id:id ::= ('a'-'z' | 'A'-'Z') ('a'-'z' | 'A'-'Z' | '0'-'9')*
|
||||
|
||||
Semantics id[[ _:id ]] : ids
|
||||
Rule id[[ Id ]] = \"Id\"
|
||||
|
||||
|
||||
Syntax Int:int ::= '0' | ('-'?_decimal)
|
||||
|
||||
Semantics int-val[[ _:int ]] : ints
|
||||
Rule int-val[[ '0' ]] = decimal-natural(\"0\")
|
||||
Rule int-val[[ Dec ]] = dec-val[[ Dec ]]
|
||||
Rule int-val[[ '-' Dec ]] = integer-negate(dec-val[[ Dec ]])
|
||||
|
||||
|
||||
Lexis Dec:decimal ::= '1'-'9'('0'-'9')*
|
||||
|
||||
Semantics dec-val[[ _:decimal ]] : ints
|
||||
Rule dec-val[[ Dec ]] = decimal-natural(\"Dec\")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,55 +1,47 @@
|
||||
Language "IBAFlang"
|
||||
|
||||
Syntax START:start ::= aexp
|
||||
Syntax START:start ::= pgm
|
||||
|
||||
Semantics start[[ _:start ]] : =>values
|
||||
Semantics start[[ _:start ]] : =>null-type
|
||||
|
||||
Rule start[[ AExp ]] =
|
||||
Rule start[[ Pgm ]] =
|
||||
initialise-binding
|
||||
finalise-failing
|
||||
eval-arith[[ AExp ]]
|
||||
|
||||
|
||||
#1 Arithmetic expressions
|
||||
run [[ Pgm ]]
|
||||
|
||||
|
||||
Syntax
|
||||
AExp : aexp ::= num
|
||||
| id
|
||||
| aexp '+' aexp
|
||||
| aexp '/' aexp
|
||||
| '(' aexp ')'
|
||||
|
||||
Semantics
|
||||
eval-arith[[ _:aexp ]] : =>integers
|
||||
Rule
|
||||
eval-arith[[ N ]] = int-val[[ N ]]
|
||||
Rule
|
||||
eval-arith[[ I ]] = assigned(bound(id[[ I ]]))
|
||||
Rule
|
||||
eval-arith[[ AExp1 '+' AExp2 ]] =
|
||||
integer-add(eval-arith[[ AExp1 ]], eval-arith[[ AExp2 ]])
|
||||
Rule
|
||||
eval-arith[[ AExp1 '/' AExp2 ]] =
|
||||
checked integer-divide(eval-arith[[ AExp1 ]], eval-arith[[ AExp2 ]])
|
||||
Rule
|
||||
eval-arith[[ '(' AExp ')' ]] = eval-arith[[ AExp ]]
|
||||
|
||||
Syntax
|
||||
N : num ::= '-'?_decimal
|
||||
Lexis
|
||||
D : decimal ::= ('0'-'9')+
|
||||
Semantics
|
||||
int-val[[ _:num ]] : integers
|
||||
Rule
|
||||
int-val[[ D ]] = decimal-natural(\"D\")
|
||||
Rule
|
||||
int-val[[ '-' D ]] = integer-negate(int-val[[ D ]])
|
||||
|
||||
Lexis
|
||||
I : id ::= ('A'-'Z'|'a'-'z')+
|
||||
Semantics
|
||||
id[[ _:id ]] : ids
|
||||
Rule
|
||||
id[[ I ]] = \"I\"
|
||||
Syntax Pgm : pgm ::= 'int' idlist ';' stmt
|
||||
|
||||
Semantics run[[ _:pgm ]] : =>null-type
|
||||
Rule run[[ 'int' IL ';' Stmt ]] =
|
||||
scope( collateral(declare-int-vars[[ IL ]]),
|
||||
execute[[ Stmt ]] )
|
||||
|
||||
Syntax IL : idlist ::= id (',' idlist)?
|
||||
|
||||
Semantics declare-int-vars[[ _: idlist ]] : (=>environments)+
|
||||
Rule declare-int-vars[[ Id ]] = bind(\"Id\", allocate-initialised-variable(integers, 0))
|
||||
Rule declare-int-vars[[ Id ',' IL ]] = declare-int-vars[[ Id ]], declare-int-vars[[ IL ]]
|
||||
|
||||
|
||||
|
||||
|
||||
Lexis keyword ::= 'else' | 'false' | 'if' | 'true' | 'while' | 'int'
|
||||
Lexis SDF
|
||||
/*
|
||||
lexical syntax
|
||||
``id`` = ``keyword`` {reject}
|
||||
lexical restrictions
|
||||
``id`` -/- [A-Za-z0-9]
|
||||
*/
|
||||
|
||||
Syntax SDF
|
||||
/*
|
||||
context-free syntax
|
||||
``exp ::= exp '+' exp`` {assoc}
|
||||
``exp ::= exp '&&' exp`` {assoc}
|
||||
``stmt ::= stmt stmt`` {right}
|
||||
*/
|
||||
41
IBAF-cbs/IBAF/IBAF-Start/IBAF-Statements.cbs
Normal file
41
IBAF-cbs/IBAF/IBAF-Start/IBAF-Statements.cbs
Normal file
@@ -0,0 +1,41 @@
|
||||
Language "IBAFlang"
|
||||
|
||||
|
||||
Syntax Stmt:stmt ::= type id '=' exp ';'
|
||||
| 'print' '(' exp ')' ';'
|
||||
// | '{' stmt '}'
|
||||
| stmt stmt
|
||||
|
||||
|
||||
Syntax Typ:type ::= 'int'
|
||||
| 'bool'
|
||||
| id
|
||||
|
||||
Semantics execute[[ _:stmt ]] : => null
|
||||
|
||||
Rule execute[[ Typ Id '=' Exp ';' ]] = assign(bound(id[[ Id ]]), eval-exp[[ Exp ]])
|
||||
Rule execute[[ 'print' '(' Exp ')' ';' ]] = print(eval-exp[[ Exp ]])
|
||||
|
||||
//Rule execute[[ '{' Stmt '}' ]] = scope(
|
||||
// collateral(collect-declared-vars[[ Stmt ]]),
|
||||
// execute[[ Stmt ]]
|
||||
//)
|
||||
|
||||
Rule execute[[ Stmt1 Stmt2 ]] = sequential(execute[[ Stmt1 ]], execute[[ Stmt2 ]])
|
||||
|
||||
|
||||
|
||||
//Semantics collect-declared-vars[[ _:stmt ]] : (=>environments)*
|
||||
//
|
||||
//
|
||||
//Rule collect-declared-vars[[ Typ Id '=' Exp ';' ]] =
|
||||
// bind(
|
||||
// \"Id\",
|
||||
// allocate-initialised-variable(
|
||||
// integers, //TODO: use type
|
||||
// eval-exp[[ Exp ]]
|
||||
// )
|
||||
// )
|
||||
//Rule collect-declared-vars[[ Stmt ]] = null
|
||||
//Rule collect-declared-vars[[ Stmt1 Stmt2 ]] = (collect-declared-vars[[ Stmt1 ]], collect-declared-vars[[ Stmt2 ]]))
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user