This section defines lexical details of the DrewScript language.
while1 is an identifier but while is not.
Examples of legal string literals:
"" "&!88" "use \n to denote a newline character" "use \" to for a quote and \\ for a backslash"
"unterminated "also unterminated \" "backslash followed by space: \ is not allowed" "bad escaped character: \a AND not terminated
=->:,+-==>>=[{<<=(!!=--++?]});/*
# this is a comment # and so is # this # and so is # this %$!#
The scanner should recognize and ignore comments (there is no COMMENT token).
For the most part, the token to produce should be self-explanatory. For
example, the
+
symbol should produce the
CROSS
token, the
-
symbol should produce the
DASH
token, etc. The set of tokens can be found in
frontend.hh
or in the switch in tokens.cpp. The LCURLY token refers to a left curly brace,
{.
the RCURLY refers to a right curly brace,
}.
The lexical structure of DrewScript is fairly straightforward, and largely similar to C. There are a few notable details, some of which are departures from C:
file produces the FILE token&& and || are NOT in the language . Instead "logical and" is represented by the string and and "logical or" is represented by the string or.
This section described the syntax of the DrewScript language.
While the canonical reference for DrewScript syntax is its context-free grammar, there are a couple of "standout" points which deserve special attention for their deviation from C:
int i; i = 4;
is not a legal program, but
int i;
void f() {
i = 4;
}
is legal.
file.
print "hello";
or
read var;
is legal.
/********************************************************************* Grammar for programs in the Drewscript language ********************************************************************/ program : globals globals : globals decl | ε decl : varDecl SEMICOL | fnDecl varDecl : type name | type name ARROW initializer type : primType LBRACKET INTLITERAL RBRACKET | primType | VOID primType : INT | BOOL | FILE fnDecl : type name LPAREN maybeFormals RPAREN LCURLY stmtList RCURLY | type name LPAREN maybeFormals RPAREN ARROW exp maybeFormals : ε | formalList formalList : formalDecl | formalList COMMA formalDecl formalDecl : type name | type name ARROW initializer stmtList : ε | stmtList stmt SEMICOL | stmtList blockStmt blockStmt : WHILE LPAREN exp RPAREN LCURLY stmtList RCURLY | IF LPAREN exp RPAREN LCURLY stmtList RCURLY | IF LPAREN exp RPAREN LCURLY stmtList RCURLY ELSE LCURLY stmtList RCURLY stmt : varDecl | loc ASSIGN exp | callExp | loc POSTDEC | loc POSTINC | READ loc | READ loc ARROW exp | PRINT exp | PRINT exp ARROW loc | RETURN exp | RETURN exp : exp DASH exp | exp CROSS exp | exp STAR exp | exp SLASH exp | exp AND exp | exp OR exp | exp EQUALS exp | exp NOTEQUALS exp | exp GREATER exp | exp GREATEREQ exp | exp LESS exp | exp LESSEQ exp | NOT exp | DASH term | term callExp : loc LPAREN RPAREN | loc LPAREN actualsList RPAREN actualsList : exp | actualsList COMMA exp term : loc | literal | LPAREN exp RPAREN | LBRACKET exp QUESTION exp COLON exp RBRACKET | callExp initializer : literal | LBRACKET litList RBRACKET litList : literal | literal COMMA litList literal : TRUE | FALSE | INTLITERAL | STRINGLITERAL loc : name | loc LBRACKET exp RBRACKET name : ID
This section defines details of the DrewScript type system.
Any type is promotable to itself.
OperandsThe following shows the atomic operands and the types that they are assigned:
intboolThe operators in the language are divided into the following categories:
[ ])=)The type rules of the language are as follows:
logical operators and conditions are legal if and only if:
The result type is bool in legal cases, ERROR otherwise.
In all illegal cases, the result type is ERROR.
relational operations are legal if and only if:
intThe result type is bool in legal cases, ERROR otherwise.
equality operations are legal if and only if:
The result type is bool in legal cases, ERROR otherwise.
assignment operations are legal if and only if:
It is LEGAL to assign arrays of the same base type but different lengths regardless if the LHS or RHS is bigger
The result type is that of the LHS operand in legal cases, ERROR otherwise.
in >> operations are legal if and only if:
int or bool
.
out << operations are legal if and only if:
intboolstringindexing operations are legal if and only if:
intThe result type is the base type of the array in legal cases, and ERROR otherwise.
function calls are legal if and only if:
If the identifier is not a function name, the result type is ERROR. Otherwise, it is the return type of the function even if the arguments are ill-typed.
function returns: The follow rules hold for function returns:
void function
may not have a value.
void
function must have a value.
It is LEGAL for a non-void function to skip a return statement. For example, this code is ok:
f : () int {
//I should return an int, but I don't
}