X-Git-Url: https://ruin.nu/git/?p=proglang.git;a=blobdiff_plain;f=documentation;h=bbb604f08eb7bb7225e8219460a812238e158726;hp=cb693df417c88f4430589c5ac0de2ce533360c91;hb=d553df8dfdffca78342d6fae142ceded9cd64415;hpb=67721d82f79e56db9c728aed76a3ad9dcca005c2 diff --git a/documentation b/documentation index cb693df..bbb604f 100644 --- a/documentation +++ b/documentation @@ -1,77 +1,143 @@ ####### DOCUMENTATIATOIAITAT ION ######## +Files: +Typechecker.hs: Simple modification of the bnfc-generated Testsyntax which calls the type-checking functions. +Typecheck.hs: Contains the type-checking functions typeCheckExp, typeCheckVar and typeCheckStm and some utility functions, responsible for the entire type-checking process. -a simple c-like language with support for if/else-statements, while-loops and the standard arithmetic (+, -, /, *) and comparison expressions (<, >, <=, >=, ==, !=). also, increase/decrease expressions (++, --) are supported. +Abssyntax.hs, Parsyntax.y, Lexsyntax.x,ErrM.hs,Printsyntax.hs,Skelsyntax.hs: The files generated by bnfc, only modification is the removal of the Bool type in Abssyntx.hs so haskell's internal type can be used. -data types: -integers and booleans. -comments: -// and /* */ comments are allowed. +typing rules +++++++++++++ +(t is used for types, T is the context, and + is used for in) +[Eq, NEq] +T+ e1 Eq e2:bool <= T+ e1:t & T+ e2:t +If e1 and e2 are of the same type, then Eq or NEq return bool +[Plus, Minus, Times, Div] +T+ e1 Plus e2:int <= T+ e1:int & T+ e2:int +The operators Plus/Minus/Times/Div return int if both operands are ints +[Lt, ELt, Gt, EGt] -examples: +T+ e1 Lt e2:bool <= T+ e1:int & T+ e2:int -fib ---- -int n1 = 0; -int n2 = 1; -int n = readInt; +The operators Lt/ELt/Gt/EGt return bool if both operands are ints -while(n-- > 0){ - int temp = n1+n2; - n1 = n2; - n2 = temp; -} -print n2; +[Assignment] -tests while, decr and assignment. +T+ i := e:t <= i:t in T & T+ e:t +The assignemnt of e to i returns type t if both i and e have type t. -if --- -if (readBool) { - if (readInt < 0) - print true; - else - print false; -} +[ENeg] +T+ ENeg e:int <= T+ e:int -tests if and if/else. +ENeg e returns int if e is of type int +[ENot] +T+ ENot e:bool <= e:bool -sum ---- -int n; -int sum = 0; -while ((n = readInt) != -1) sum = sum + n; -print sum; +ENot e returns bool if e is of type bool +[EVar] +T+ i:t <= i:t in T +i has type t if i is defined in the context with type t. -var ---- -int a = 3; -int b = a - 5; -int c = a + b*7; -bool d = a == b; +[EInt] -tests simple variable operations. \ No newline at end of file +T+ n:int + +n has type int + +[EBool] + +T+ b:bool + +b has type bool + +[EReadI] + +T+ n:int + +EReadI returns an int + +[EReadB] + +T+ b:bool + +EReadB returns a bool + +[EPost] + +T+ EPost i:int <= i:int in T + +EPost i is of type int if i is defined in T with type int. + +[SExp] + +T+ e <= T+ e:t + +[SBlock] + +T+ s;SBlock ss <= T+ s => T' , T'+ ss => T'' + +the first statment s, in the block, is typechecked in the context T and returns the context T', the rest of the block is then recursively typeckecked in the context T' + + +[SIf] + +T+ if e then s1 else s2 <= T+ e:bool & T+ s1 & T+ s2 + +if e is of type bool and s1 and and s2 typechecks in the context T, then the same context is returned + + +[SWhile] + +T+ while e do s <= T+ e:bool & T+ s + +If e is of type bool and s typechecks in context T then the same context is returned + + + +[SDecl] + +T+ t i = e => T,i:t <= i not in T & e:t + +if i and e are of the same type and i is not declared in the current scope then i is added with type t to the context. + +[SDeclD] + +T+ t i => T,i:t <= i not in T + +if i is not declared in the current scope, then i is added to the context with type t + +[SNoop] + +T+ s + +SNoops does nothing so the same context is returned + +[SPrint] + +T+ e <= T+ e:t + +if e has type t then SPrint returns the same context