Kzero

a Programming Language

Clinton Jeffery clinton.jeffery@nmt.edu
with input from CSE 423 students

Draft Version 0.10, March 4, 2025.



Language Reference Manual


Abstract

k0 (pronounced "Kay Zero", short for Kotlin 0 Compiler) is a subset of the Kotlin Programming Language. k0 is a tiny language intended to be implemented in a compiler construction class.





New Mexico Institute of Mining and Technology
Department of Computer Science and Engineering
Socorro, NM 87801 USA












Contents

  1. Introduction
  2. Lexical Rules
  3. Syntax
  4. Data Types and Semantics
  5. Summary


1. Introduction

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.

2. Lexical Rules

The lexical rules of k0 start with: the lexical rules of Kotlin. k0 may simplify and reduce the lexical rules of Kotlin a bit.

2.1 Whitespace and Comments

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 */

2.2 Reserved Words

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.

2.3 Operators

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

2.4 Literals

Integers

Reals

Characters

Strings

Escape Sequences (Character and String Literals)

k0 supports \n, \t, \', \", \r, \0, \b, \$, \\
Unsupported escapes (backslash followed by anything else) should be recognized and report a lexical error.

Escape sequenceHex 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

2.5 Punctuation

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
#  $  @  \  `
  

2.6 Identifiers

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.

3. Syntax

A good fraction of standard Kotlin syntax will denote constructs that are not supported in k0. The easiest thing is probably to support the whole Kotlin language grammar, less the parts that have been ruled out via lexical errors, and then define portions of it that will be unsupported and trigger an error in HW#3.

3.1 Function Syntax

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.

3.2 Control Structures

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.

3.3 Classes

k0 has no class, other than references to instances of a few library classes.

k0 does not have anonymous (non-labeled or lambda) classes.

3.4 Declaration Syntax

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 = literal
A 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.

4. Data Types

4.1 Numbers

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.

4.2 Strings

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.

4.3 Arrays

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.

4. Library Functions

k0 supports a small subset of Kotlin's standard libraries. Pretty much I understand these as being the Kotlin version of the Java equivalents. I haven't looked up all the Kotlin versions yet, but here is what I think k0 should include:

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()

Summary

Sure, k0 may be a toy language designed for a compiler class. Even with only this much, it may provide a convenient notation for a lot of simple programming tasks such as those faced by students in CSE 113.