clinton.jeffery@nmt.edu
k0(pronounced "Kay Zero", short for Kotlin 0 Compiler) is a subset of the Kotlin Programming Language.k0is a tiny language intended to be implemented in a compiler construction class.
k0 is a subset of Kotlin.
Broadly, k0 is intended to correspond roughly to
the subset of Kotlin that would be covered in a CS1 class.
The facilities that k0 supports are interesting
enough to write some non-trivial computations in it.
k0 programs are legal Kotlin programs with a .kt
file extension. The only form of Kotlin program supported in
a k0 program consists of a main() function where
execution starts, followed by zero or more called functions. A "Hello
world" program looks like:
fun main(args : Array<String>) {
println("Hello, World!")
}
Kotlin features many basic types.
k0 supports:
Byte Short Int Long Float Double Boolean String
The types Int and Double both refer to 64-bit values.
The types Short, Long, and Float are
allowed in k0 and denote aliases for Int and Double.
Boolean and Byte are stored in a single byte (like C, not like Java with its
16-bit characters)
k0 has while and supports a small subset of
the Kotlin for loops.
Curly braces around the loop body are required.
The only types of for loops in
k0 are the ones with explicit integer ranges:
for (i in x..y)
Conditionals in k0 consist of if statements.
if statements use syntax similar to while loops.
Curly braces are required.
An else branch is optional.
if (x < 0) {
...
}
else branches require curly braces,
unless they are (chained) if statements.
if (x < 0) {
...
} else if (x < 10) {
...
} else {
...
}
Kotlin supports creation of new types via a class.
k0 does not have (user-defined) classes, but does support
creation and manipulation of instances of certain library classes.
Kotlin has weird arrays, via some library classes.
k0 has one-dimensional arrays only.
When in doubt about k0 features,
refer to the Kotlin language specification.
I will add notes below as needed. The easiest way to get out of
having to implement something is to ask about it and negotiate.
k0 start with: the lexical rules of Kotlin.
k0
may simplify and reduce the lexical rules of Kotlin a bit.
Of the Kotlin whitespace characters, k0 must implement space, tab, carriage return, and newline.
k0 supports both styles of Kotlin comments. Comments may be text placed between the delimiters /* and */. They may not be nested. Comments can also use // to comment from that point to the end of a line. Examples:
x = 1; //single line comment
/* this is a
multiple line
comment */
Kotlin has many reserved words. The reserved words (also called keywords) in all of k0 are in bold. Those not in any k0 have strikethrough and should result in a fatal error ("this Kotlin feature is not in k0").
Supported reserved words: break, continue, do, else, false, for, fun, if, in, null, return, true, val, var, when, while,
Kotlin reserved words not in Kzero:
as, as?, class, !in, is, !is, object, package, super, this, throw, try,
typealias, typeof
.
Regarding the "Soft" keywords, k0 treats all soft keywords as
hard keywords (reserved words, never identifiers).
Supported "soft" keywords:
import
Kotlin "soft" keywords words not in k0:
by, catch, constructor, delegate, dynamic, field, file, finally, get,
init, param, property, receiver, set, setparam, value, where
.
Regarding the "Modifier" keywords, k0
will generally just not do modifier keywords unless someone
persuades me otherwise. But I suppose we might need const
Supported Modifier keywords:
const
Kotlin Modifier keywords words not in k0:
abstract, actual, annotation, companion, crossinline, data, enum, expect,
external, final infix, inline, inner, internal, lateinit, noinline,
open, operator, out, private, protected, public, reified, sealed, suspend,
tailrec, vararg
.
Regarding special identifiers,
field and it will be reserved words that
are k0 errors.
k0 supports the following operators
| = += -= | assignment, augmented add and subtract |
| + - * / % | binary arithmetic, int and float |
| ++ -- | unary increment and decrement, int only, postfix only |
| - | unary negation, prefix |
| == != > < >= <= | binary comparison |
| === !== | referential equality |
| && || ! | logical AND, OR, and NOT |
| !! | unary, asserts an expression is non-nullable |
| [ ] . | binary subscript and dot |
| ?. | perform method call or object field IF non-nullable |
| ?: | produce right side if left side is null |
| ? | unary suffixes produces nullable type |
| .. ..< | ranges. k0 usage restricted from full Kotlin |
| (type) | basic type casts |
An error is reported for
k0 does not bother with hex and binary.
Neither k0 nor Kotlin does octals.
k0 only does simple real literals involving
one or more integers to the left or right (or both) of a decimal
k0 shall not bother with scientific/exponent
k0 allows simple character literals consisting of a single
character enclosed in apostrophes
k0 recognizes and reports an error for character literals
with more than one character, e.g. 'abc'
k0 allows simple string literals consisting of zero or more
characters enclosed in double quotes
| Escape sequence | Hex value in ASCII | Character represented |
|---|---|---|
| \b | 08 | Backspace |
| \n | 0A | Newline (Line Feed); see notes below |
| \r | 0D | Carriage Return |
| \t | 09 | Horizontal Tab |
| \\ | 5C | Backslash |
| \' | 27 | Apostrophe or single quotation mark |
| \" | 22 | Double quotation mark |
| \$ | dollar sign |
Punctuation characters are lexemes that are supported in k0 that are not part of other lexemes (not operators, not identifiers, not literals).
( ) , ; { } , :
Other punctuation characters generally should be reported as lexical errors,
including
# $ @ \ `
Identifiers in k0 are as per the C language, not Kotlin: a letter followed by zero or more additional letters or digits. They are case-sensitive.
Function definitions in k0 follow the following format. They are all static, with the static keyword required before the return type.
fun identifier ( parameter_list ): return type
{ function body }
k0 does not support function bodies that are expressions not
enclosed in curly brackets. The colon and the return type can be omitted
for functions with no return, as per Kotlin.
if (statement) {}
if (statement) {} else {}
if (statement) {} else if (statement) {} else {}
while (statement) {}
for ( x in y..z) {}
Note we are not including the multi-faceted Kotlin when structure.
k0 has no class, other than references to instances of a few library classes.
k0 does not have anonymous (non-labeled or lambda) classes.
var or val is used to declare a variable
or a constant, respectively.
In k0,
declaration syntax is only allowed for global variables and at the top of
the bodies of function definitions, before the first executable statement.
k0 allows only simple initializers including int, float and char.
var identifier : type val identifier : type = literalA slightly simpler syntax is allowed for parameter lists, which do not allow initializers.
In k0, variable declarations may
be comma-separated lists of identifiers, each of which may have array or
initializer suffixes.
All data types listed are used by k0 and are described in the reserved words section.
Although the C language automatically converts between numeric types as needed (promotion and demotion), k0 does not.
These are references to a String class, as per Java. strings are immutable, they are objects not arrays of char or pointer to char, etc.
k0 supports only simple single dimensional arrays.
For example:
var num : Array<Int> = Array<Int> (100) { 0 }
Array elements are read (and written, if the array is var) using subscripts.
print(s) println(s) readln() String.get(n) String.equals(s) String.length() String.toString(i) vs. String.valueOf() ?? String.substring(x,y) java.util.Random.nextInt() java.lang.Math.abs() java.lang.Math.max() java.lang.Math.min() java.lang.Math.pow() java.lang.Math.cos() java.lang.Math.sin() java.lang.Math.tan()