Skip to content

Latest commit

 

History

History
355 lines (249 loc) · 5.47 KB

SPEC.md

File metadata and controls

355 lines (249 loc) · 5.47 KB

kou Language Specification

Introduction

This document is a language specification (yet informal) of the kou programming language.

Notation

The syntax is specified using Extended Backus-Naur Form (EBNF).

|   alternation
()  grouping
[]  option (0 or 1 times)
{}  repetition (0 to n times)

Lower-case production names are used to identify lexical tokens. Non-terminals are in CamelCase. Lexical tokens are enclosed in double quotes "".

Lexical elements

Punctuation

-> , ( ) [ ] { } : = ;

Operators

Unary:

unary_op = "+" | "-" | "!" .

Binary:

binary_op = rel_op | add_op | mul_op | bool_op .
rel_op = "==" | "!=" | "<" | "<=" | ">" | ">=" .
add_op = "+" | "-" | "|" | "^" .
mul_op = "*" | "/" | "%" | "&" .
bool_op = "||" | "&&" .

Keywords

import as let fn if else for in new while break

Literals

Integer:

decimal_digit = "0" … "9" .
int_lit = decimal_digit { decimal_digit } .

Float:

decimals  = decimal_digit { decimal_digit } .
float_lit = decimals "." [ decimals ]
          | "." decimals

Char:

escaped_char = "\" ( "n" | "r" | "t" | "\" | "'" | """ ) .
char = unicode_char | escaped_char .
char_lit = "'" ( char ) "'"

String:

string_lit = """ { char } """ .

Boolean:

bool_lit = "true" | "false"

Identifier

lower_letter = "a" … "z" .
letter = lower_letter | "_" .
ident = letter { letter | decimal_digit } .

Types

Type = PrimType | FuncType | TupleType | ArrayType | VoidType .

Primary types

PrimType = "int" | "float" | "str" | "bool" | "char" .

Function type

FuncType = Type "->" Type .

Tuple type

TupleType = "(" [ Type { "," Type } ] ")" .

Semantically, 1-tuple is the same with its inner type, or 1-tuple is desugared into its inner type.

Related: TupleExpr

Array type

ArrayType = "[" Type "]" .

Related: ArrayExpr

Void type

VoidType = "void" .

Void type does not have a value. Any actual value in the type of "void" should result in a semantic error.

Module

Each file in kou is represented as a module.

Module = { Import } { Decl } .

Import

Import = "import" ImportPath
         "(" ImportElem { "," ImportElem } ")" .
ImportPath = string_lit .
ImportElem = ident [ "as" ident ] .

Declaration

Decl = "let" ident [ ":" Type ] "=" Expr .

Expressions

Expr = PrimUnaryExpr | BinaryExpr .
BinaryExpr = Expr binary_op Expr .
PrimUnaryExpr = PrimExpr | UnaryExpr .
UnaryExpr = unary_op PrimUnaryExpr
PrimExpr = LitExpr
         | IdentExpr
         | TupleExpr
         | ArrayExpr
         | CallExpr
         | FuncExpr
         | CondExpr
         | LoopExpr
         | NewExpr.

Expr stands for Expression.

LitExpr

The name stands for Literal Expression.

LitExpr = int_lit | float_lit | string_lit | bool_lit | char_lit .

IdentExpr

The name stands for Identifier Expression.

IdentExpr = ident .

TupleExpr

TupleExpr = "(" [ Expr { "," Expr } ] ")" .

Semantically, 1-tuple is the same with its inner value, or 1-tuple is desugared into its inner value.

Related: Tuple type

ArrayExpr

ArrayExpr = "[" Expr { "," Expr } "]"

Related: Array type

CallExpr

CallExpr = PrimExpr TupleExpr .

Related: TupleExpr

IndexExpr

IndexExpr = PrimExpr "[" Expr "]" .

It can be used to retrieve an element from an array or a tuple.

For the tuple case, the index should be a LitExpr having int_lit, with a value in the tuple's size range.

Related: Literals

FuncExpr

FuncExpr = "fn" ParamTuple Type Block .
ParamTuple = "(" [ Param { "," Param } ] ")" .
Param = ident Type .

Related: Block

CondExpr

CondExpr = "if" Expr Block "else" Block .

Related: Block

LoopExpr

LoopExpr = "while" Expr Block .

Related:

NewExpr

NewExpr = "new" Type "[" Expr "]" .

It creates an array with a specified size.

Related:

Assignment

Assign = LVal "=" Expr .

LVal

LVal = IdentExpr
     | IndexExpr .

Related:

Break

Break = "break" .

Break only works in LoopExpr.

Related: LoopExpr

Block

Block = "{" { ( Expr | Decl | Assign | Break ) ";" } [ Expr ] "}" .

A block ending without Expr (no ";") has its return type as void, and it is the only way to express void type in kou.

Related: Void type