Skip to content

Specification

Felix Schoeller edited this page Sep 8, 2020 · 9 revisions

Kantan Specification

Introduction

This is the reference for the Kantan programming language.

Kantan is a system programming language with the goal of being a modernized version of C. The language is primarily designed for C programmers who want the advancements in programming language design of the last few decades in their language.

Lexical elements

Keywords

The following keywords cannot be used as identifiers.

let return def delegate defer if else import extern export while for struct union enum null undefined new delete sizeof as break continue type

The following keywords are treated in a special way, but are context sensitive and therefore usable as identifiers.

true false

Comments

Kantan only specifies line comments, which have to start with // and reach until the next new line character.

Builtin types

Kantan has the following primitive types. These names are not keywords and therefore can be used as identifiers

i8 i16 i32 i64 isize u8 u16 u32 u64 usize f32 f64 string bool void

string is just an alias for *i8

Grammar

Module = { TopLevelStatement };
TopLevelStatement = FunctionDefinition | TypeDeclaration | ImportStatement | VarDeclStatement;

Statement = CompoundStatement | SimpleStatement ";";
CompoundStatement = IfStmt | BlockStmt | WhileStmt | ForStmt;
IfStmt = "if" Expression BlockStmt { "else" (IfStmt | BlockStmt) };
WhileStmt = "while" Expression BlockStmt;
ForStmt = "for" { SimpleStatement } ";" Expression ";" { SimpleStatement };
BlockStmt = "{" { Statement } "}";

SimpleStatement = VarDeclStatement | ReturnStatement | ControlFlowStatement | DeferableStatement | DeferStatement | ImportStatement;
DeferStatement = "defer" DeferableStatement;
DeferableStatement = DeleteStatement | ExpressionStatement;
ControlFlowStatement = "continue" | "break";
VarDeclStatement = "let" Identifier [ ":" TypeIdentifier ] "=" Expression;
DeleteStatement = "delete" Expression;
ReturnStatement = "return" Expression;
ExpressionStatement = Expression;

TypeIdentifier = NamedTypeIdentifier | PointerTypeIdentifier | ArrayTypeIdentifier;
ArrayTypeIdentifier = "[" Expression "]" TypeIdentifier;
PointerTypeIdentifier = "*" TypeIdentifier;
NamedTypeIdentifier = Identifier;

FunctionDefinition = ExternDefinition | DelegateDefinition | NormalDefinition;
NormalDefinition = ["export"] FunctionSignature BlockStmt;
ExternDefinition = "extern" FunctionSignature ";";
DelegateDefinition = "delegate" FunctionSignature ";";
FunctionSignature = "def" [ "(" Parameter ")" ] Identifier "(" [ ParameterList ] ")" [":" TypeIdentifier ];
ParameterList = Parameter { "," Parameter };
Parameter = Identifier ":" TypeIdentifier;

TypeDeclaration = "type" Identifier (StructDeclaration | EnumDeclaration | UnionDeclaration);
StructDeclaration = "struct" "{" [ FieldList ] "}";
UnionDeclaration = "union" "{" [ FieldList ] "}";
FieldList = Field { "," Field };
Field = Identifier ":" TypeIdentifier ;

EnumDeclaration = "enum" "{" [ EnumValueList ] "}";
EnumValueList = EnumValue [ "=" StartValue ] { "," EnumValue };
EnumValue = Identifier;
StartValue = LogicOr;

ImportStatement = "import" StringLiteral [ "as" Identifier ] ";";

Expression = Assignment | LogicOr;
Assignment = Expression ("=" | "+=" | "-=" | "*=" | "/=" | "%=") Expression;
LogicOr = LogicAnd { "||" LogicAnd };
LogicAnd = Equality { "&&" Equality };
Equality = Comparison { ("==" | "!=") Comparison };
Comparison = Addition { ("<" | "<=" | ">=" | ">") Addition };
Addition = Multiplication { ("+" | "-") Multiplication };
Multiplication = Unary { ( "*" | "/" | "%" | "as" | "<<" | ">>" ) Unary };
Unary = ("!" | "-" | "*" | "&") Unary | Call;
Call = Unit { "(" [ Arguments ] ")" | "." Identifier };
Unit = "true" | "false" | "null" | "undefined" | NumberLiteral | StringLiteral | Identifier | CharLiteral | "(" Expression ")" | "sizeof" TypeIdentifier | "new" Expression | Identifier "{" [ InitList ] "}" | ArrayTypeIdentifier "{" [ InitList ] "}";

InitList = FieldInit { "," FieldInit };
FieldInit = Identifier ":" Expression;

Arguments = Expression { "," Arguments };

Identifier = "_" [ { "_" | Digit } Identifier ] | Letter { Letter | Digit | "_" };
Letter = "a".."z" | "A".."Z";
Digit = "0".."9";

StringLiteral = "\"" { Char } "\"";
CharLiteral = "'" Char "'";
NumberLiteral = IntLiteral [ "." IntLiteral ]);
IntLiteral = Digit { Digit };
Char = A single unicode code point;
Clone this wiki locally