Documentation - shreeyagad/oop-lang GitHub Wiki

Literals

  • int β†’ integer values, i.e. 1, 2.
  • boolean β†’ true or false
  • String β†’ sequences of characters, i.e. β€œHello world!”
  • null

Expressions

  • Arithmetic β†’ adding/subtracting/multiplying/dividing values of type int, i.e. 1 + 2, 1 + (2 * 3), 1 - (2 / 3)

  • Boolean β†’ applying logical/comparison operators to values of type boolean, i.e. true && false, !(1 > 2), !(3 < 4) || (5 < 6)

  • String β†’ uses β€œ+” operator to support concatenation between values of type String

  • Assignment β†’ assign values to new or existing variables in the current environment

  • If β†’ evaluates true branch if condition is true and else branch if condition is false

  • While β†’ while condition is true, evaluates the body

  • Break β†’ when used within a while loop, breaks the loop and continues evaluation of the program with the next statement

  • Function Declaration β†’ maps the function name to a new function with specified arguments and body in the current environment

  • Function Call β†’ retrieves a declared function from the current environment and evaluates the body with the provided argument values

  • Print β†’ prints the right-hand expression of the keyword β€œprint” to the console

  • Return β†’ can only be used in a function body to break the control flow and return the right-hand expression

  • Class Declaration β†’ maps the class name to a new class with specified attributes and methods in the current environment

  • Constructor β†’ instantiates a new object of the class, maps the argument values to the object’s attributes, and returns the object

  • Attribute Call β†’ returns the specified attribute of the object

  • Method Call β†’ calls the specified method on the object

Grammar

type ::= int | boolean | String | ClassName

program ::= [statement]*

statement ::= expr;

expr ::= SimpleExpression | CommandExpression

SimpleExpression ::= ArithExpression | BoolExpression | StringExpression | ObjExpression

ArithExpression ::= x | n | n + m | n - m | n * m | n / m

BoolExpression ::= x | true | false | b1 && b2 | b1 || b2 | !b1 | n > m | n >= m | n == m | n <= m | n < m

StringExpression ::= x | s | s1 + s2

ObjExpression ::= x | new ClassName([SimpleExpression,]*)

CommandExpression ::= AssnExpression | IfExpression | WhileExpression | BreakExpression | FunctionDeclExpression | FunctionCallExpression | PrintExpression | ReturnExpression | ClassDeclExpression | AttributeCallExpression | MethodCallExpression

AssnExpression ::= int identifier = ArithExpression | boolean identifier = BoolExpression | String identifier = StringExpression | ClassName identifier = ObjExpression

IfExpression ::= if (BooleanExpression) { program } | if (BooleanExpression) { program1 } else { program2 }

WhileExpression ::= while (BooleanExpression) { program }

BreakExpression ::= break

FuncDeclExpression ::= function identifier( [type argIdentifier,]* ) { program }

FuncCallExpression ::= identifier( [SimpleExpression,]* )

PrintExpression ::= print(expr)

ReturnExpression ::= return expr

ClassDeclExpression ::= class identifier { [type typeIdentifier;]* function identifier( [type argIdentifier,]* ) { [super([SimpleExpression,]*);] program } [FuncDeclExpression;] }

AttributeCallExpression ::= identifier.identifier[.identifier_n]*

MethodCallExpression ::= identifier.FuncCallExpression

Notes:

  • x represents a variable in the environment
  • identifier can only consist of numbers, letters, underscores, and ampersands
  • return should only be used within the body of a function/method
  • break should only be used within the body of a while loop
  • the first method in a class declaration acts as the class constructor and must have the same name as the class
  • ClassName is the name of a user-defined class