COMS W4115Language Reference ManualVence Stanev (UIN: vs2226)Turn based simulation language TBSLAbstractThe turn based simulation language (TBSL) is a functional language that enablesprogrammers to describe a current state of a system comprised of objects. The goal of TBSLis to run that simulation for a number of turns in order to examine the effects of particularphenomena on the system.ApplicationsAmong other things, TBSL can be used to describe a group of business entities withdifferent strategies and observe the effect over time.1. Lexical Conventions1.1 Identifiers - An identifier is a sequence of letters, digits and the underscorecharacter. Each identifier starts with a letter. Identifiers are case sensitive -upper and lower case letters are considered different.1.2 Comments – Comments are introduced with the opening character sequence/* and closed with the sequence */. Comments cannot be nested - thecharacters /* introduce a comment, which terminates with the firstoccurrence of the characters */.1.3 Keywords - Keywords are identifiers that are reserved words in TBSL. Theyhave specific function and cannot be used as regular identifiers.Init – initialize an objectRelation - define a relationFunc – define a functionList – define a list of “Objects”Turns – makes the simulation go to the next turnCOMS W41151.4 Operators1.5 Punctuation1.6 Constants – constants are used to initialize variable attributes.1.6.1 Integer constants – integer constants are represented with wholenumbers in decimal format. An integer constant constitutes only ofdigits; decimal point and exponent are not allowed. A unary – operatoris allowed. An example of an integer constant is 4 or 6000 or 12. Thesystem stores all numbers as floating point numbers so each integerconstant is implicitly converted to a float.1.6.2 Floating point constants – floating point constants are representedwith a whole part, a decimal point and a fractional part. The whole partand the fractional part are made up only of digits. A unary – operatoris allowed. An example of a floating point constant is 5.3 or 0.12345.1.6.3 String constants – string constants are made up of a sequence ofcharacters that are enclosed in quotes. For example “this is a string”or “5” or “Some characters @#$%^&( “.2. Basic types - TBSL has only one basic type, which is called “Object”. No notion of typeconversion is defined. TBSL also supports lists of “Objects”.2.1 “Object” type - When declaring a variable, type is not specified but thevariable needs to be initialized. A variable is initialized by providing a customlist of attributes, which is a list of tuples, each tuple being a name\value pair.The name is always a string and the value can be an int, float or string.Defining a second variable with the same name in the same scope is notallowed. A variable has no predefined attributes. Attributes are all custom andcould be added at initialization time as well as later in the program.Syntax example:Init a ((“status”,”active”), (“cost”, 5.7), (“ValueAddPerTurn”,10));Punctuation Use Example/* */ Comments /* This is a comment */“ ” String constant “This is a string”; Indicates the end of a statement Compare (a,b);, Argument list separator Compare (a,b);() Argument list delimiter Compare (a,b);{} Function body or block ofstatementsFunc Compare (a,b){Body of function here}-> Reference a variable attribute a->costCOMS W4115This syntax initializes the variable a.Syntax example:Attribute(a, (“cost”, 5.7));This syntax will add the “cost” attribute to the “a” variable if the attributedoesn’t already exist and it will update it if it does.2.2 Reference a variable attribute – A variable attribute could be referenced byproviding the following syntax:Syntax example:a->cost2.3 List of “Objects” – TBSL supports grouping of variables in a list.Syntax example:List ObjList; ObjList.Append(a); ObjList.Prepend(a);ObjList.Remove(a);3. Operators - Operators in TBSL are tokens that allow for particular operations on data.The standard Math operators are available ( i.e. +, -, *,/ ) as well as the logicaloperators AND and OR (i.e. &,|). In addition the brackets operator (i.e. ( ) ) is alsoavailable. These operators are defined for variable attributes and are ranked byprecedence.Syntax example:Init a ((“status”,”active”), (“cost”, 5.7), (“ValueAddPerTurn”,10));Init b ((“status”,”inactive”), (“cost”, 4.0), (“ValueAddPerTurn”,12));/* Addition*/Attribute (a, (“cost”, a->cost+3));/* concatenation */Attribute (a, (“cost”, “foo” +”bar”));COMS W41154. Syntactic constructs – TBSL supports the following control constructs4.1.If than else – conditional control logicSyntax example:If ( a->cost >3) thenAttribute (a, (“cost”, 1003));ElseAttribute (a, (“cost”, a->cost+1));4.2.LoopsSyntax example:Attribute (a, (“cost”, 0));While(a->cost <10){Attribute (a, (“cost”, a->cost+1));}5. Functions -TBSL supports functions in order to promote modularity. A function is acollection of statements that are given a name. Functions in TBSL do not have a returntype; all parameters are “passed by reference” and the outcome of the function isreflected directly on the input.Syntax example:Func MyFunciton (ListOfObjects){Init a ((“status”,”active”), (“cost”, 5.7), (“ValueAddPerTurn”,10));Init b ((“status”,”inactive”), (“cost”, 4.0), (“ValueAddPerTurn”,12));ListOfObjects.Append(a);ListOfObjects.Append(b);}COMS W41156. Scope – TBSL supports the notion of scope by defining blocks of code much like C andJava do. A block of code is defined by wrapping it in {}.7. Example of an AlgorithmInit simulation ((“turns”,10)(“turnDecrement”,1));Init store_a ((“status”,”active”), (“balance”, 7.2), (“ValueAdd”,10));Init store_b ((“status”,”inactive”), (“balance”, 4.0), (“ValueAdd”,12));While (simulation->turns >0){Attribute (store_a, (“balance”, store_a->balance+store_a->ValueAdd));Attribute (store_b, (“balance”, store_b->balance+store_b->ValueAdd));Attribute (simulation, (“turns”, simulation->turns – simulation->turnsDecrement));}/* Prints all attributes of the object
View Full Document