41 Commits

Author SHA1 Message Date
21228ff3d7 Implement vec based scopes
- Replaced vartable hashmap with vec
- Use linear search in reverse to find the variables by name
- This is really fast with a small number of variables but tanks fast
  with more vars due to O(n) lookup times
- Implemented scopes by dropping all elements from the vartable at the
  end of a scope
2022-02-03 22:09:58 +01:00
588b3b5b2c Autoformat 2022-02-03 17:38:25 +01:00
f6152670aa Small refactor for lexer 2022-02-03 17:25:55 +01:00
c2b9ee71b8 Add project euler example 5 2022-02-03 16:16:38 +01:00
f8e5bd7423 Add comments to parser 2022-02-03 16:01:33 +01:00
d7001a5c52 Refactor, Comments, Bugfix for lexer
- Small refactoring in the lexer
- Added some more comments to the lexer
- Fixed endless loop when encountering comment in last line
2022-02-03 00:44:48 +01:00
bc68d9fa49 Add Result + Err to lexer 2022-02-02 21:59:46 +01:00
264d8f92f4 Update README 2022-02-02 19:40:10 +01:00
d8f5b876ac Implement String Literals
- String literals can be stored in variables, but are fully immutable
  and are not compatible with any operators
2022-02-02 19:38:28 +01:00
8cf6177cbc Update README 2022-02-02 19:15:20 +01:00
39bd4400b4 Implement logical not 2022-02-02 19:14:11 +01:00
75b99869d4 Rework README
- Add full language description
- Fix variable name inconsistency
2022-02-02 19:00:14 +01:00
de0bbb8171 Implement logical and / or 2022-02-02 18:56:45 +01:00
92f59cbf9a Update README 2022-02-02 16:48:26 +01:00
dd9ca660cc Move ast into separate file 2022-02-02 16:43:14 +01:00
7e2ef49481 Move token into separate file 2022-02-02 16:40:05 +01:00
86130984e2 Add example programs (project euler) 2022-02-02 16:26:37 +01:00
c4b146c325 Refactor interpreter to use borrowed Ast
- Should have been like this from the start
- About 9x performance increase
2022-02-02 16:24:42 +01:00
7b6fc89fb7 Implement if 2022-02-02 16:19:46 +01:00
8c9756b6d2 Implement print keyword 2022-02-02 14:05:58 +01:00
02993142df Update README 2022-01-31 23:49:22 +01:00
3348b7cf6d Implement loop keyword
- Loop is a combination of `while` and `for`
- `loop cond { }` acts exactly like `while`
- `loop cond; advance { }` acts like `for` without init
2022-01-31 16:58:46 +01:00
3098dc7e0a Implement simple CLI
- Implement running files
- Implement interactive mode
- Enable printing tokens & ast with flags
2022-01-31 16:24:25 +01:00
e0c00019ff Implement line comments 2022-01-29 23:29:09 +01:00
35fbae8ab9 Implement multi statement code
- Add statements
- Add mandatory semicolons after statements
2022-01-29 23:18:15 +01:00
23d336d63e Implement variables
- Assignment
- Declaration
- Identifier lexing
2022-01-29 22:49:15 +01:00
39351e1131 Slightly refactor lexer 2022-01-29 21:59:48 +01:00
b7872da3ea Move grammar def. to README 2022-01-29 21:54:05 +01:00
5cc89b855a Update grammar 2022-01-29 21:52:31 +01:00
32e4f1ea4f Implement relational binops 2022-01-29 21:48:55 +01:00
b664297c73 Implement comparison binops 2022-01-29 21:37:44 +01:00
ea60f17647 Implement bitwise not 2022-01-29 21:26:14 +01:00
5ffa0ea2ec Update README 2022-01-29 21:18:08 +01:00
2a59fe8c84 Implement unary negate 2022-01-29 21:12:01 +01:00
8f79440219 Update README 2022-01-29 20:52:30 +01:00
128b05b8a8 Implement parenthesis grouping 2022-01-29 20:51:55 +01:00
a9ee8eb66c Update grammar definition 2022-01-28 14:00:51 +01:00
5c7b6a7b41 Update README 2022-01-28 12:20:59 +01:00
a569781691 Implement more operators
- Mod
- Bitwise Or
- Bitwise And
- Bitwise Xor
- Shift Left
- Shift Right
2022-01-27 23:15:16 +01:00
0b75c30784 Implement div & sub 2022-01-27 22:29:06 +01:00
ed2ae144dd Number separator _ 2022-01-27 21:38:58 +01:00
15 changed files with 1288 additions and 144 deletions

View File

@@ -4,3 +4,5 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
anyhow = "1.0.53"
thiserror = "1.0.30"

211
README.md
View File

@@ -1,7 +1,212 @@
# NEK-Lang # NEK-Lang
## Variables
Currently all variables are global and completely unscoped. That means no matter where a variable is declared, it remains over the whole remaining runtime of the progam.
All variables are currently of type `i64` (64-bit signed integer)
### Declaration
- Declare and initialize a new variable
- Declaring a previously declared variable again is currently equivalent to an assignment
- Declaration is needed before assignment or other usage
- The variable name is on the left side of the `<-` operator
- The assigned value is on the right side and can be any expression
```
a <- 123;
```
Create a new variable named `a` and assign the value `123` to it.
### Assignment
- Assigning a value to a previously declared variable
- The variable name is on the left side of the `=` operator
- The assigned value is on the right side and can be any expression
```
a = 123;
```
The value `123` is assigned to the variable named `a`. `a` needs to be declared before this.
## Expressions
The operator precedence is the same order as in `C` for all implemented operators.
Refer to the
[C Operator Precedence Table](https://en.cppreference.com/w/c/language/operator_precedence)
to see the different precedences.
### General
- Parentheses `(` and `)` can be used to modify evaluation oder just like in any other
programming language.
- For example `(a + b) * c` will evaluate the addition before the multiplication, despite the multiplication having higher binding power
### Mathematical Operators
Supported mathematical operations:
- Addition `a + b`
- Subtraction `a - b`
- Multiplication `a * b`
- Division `a / b`
- Modulo `a % b`
- Negation `-a`
### Bitwise Operators
- And `a & b`
- Or `a | b`
- Xor `a ^ b`
- Bitshift left (by `b` bits) `a << b`
- Bitshift right (by `b` bits) `a >> b`
- "Bit flip" (One's complement) `~a`
### Logical Operators
The logical operators evaluate the operands as `false` if they are equal to `0` and `true` if they are not equal to `0`
- And `a && b`
- Or `a || b`
- Not `!a` (if `a` is equal to `0`, the result is `1`, otherwise the result is `0`)
### Equality & Relational Operators
The equality and relational operations result in `1` if the condition is evaluated as `true` and in `0` if the condition is evaluated as `false`.
- Equality `a == b`
- Inequality `a != b`
- Greater than `a > b`
- Greater or equal than `a >= b`
- Less than `a < b`
- Less or equal than `a <= b`
## Control-Flow
For conditions like in if or loops, every non zero value is equal to `true`, and `0` is `false`.
### Loop
- There is currently only the `loop` keyword that can act like a `while` with optional advancement (an expression that is executed after the loop body)
- The `loop` keyword is followed by the condition (an expression) without needing parentheses
- *Optional:* If there is a `;` after the condition, there must be another expression which is used as the advancement
- The loops body is wrapped in braces (`{ }`) just like in C/C++
```
// Print the numbers from 0 to 9
// Without advancement
i <- 0;
loop i < 10 {
print i;
i = i - 1;
}
// With advancement
k <- 0;
loop k < 10; k = k - 1 {
print k;
}
```
### If / Else
- The language supports `if` and an optional `else`
- After the `if` keyword must be the deciding condition, parentheses are not needed
- The block *if-true* block is wrapped in braces (`{ }`)
- *Optional:* If there is an `else` after the *if-block*, there must be a following *if-false*, aka. else block
```
a <- 1;
b <- 2;
if a == b {
// a is equal to b
print 1;
} else {
// a is not equal to b
print 0;
}
```
## IO
### Print
Printing is implemented via the `print` keyword
- The `print` keyword is followed by an expression, the value of which will be printed to the terminal.
- Print currently automatically adds a linebreak
```
a <- 1;
print a; // Outputs `"1\n"` to the terminal
```
## Comments
### Line comments
Line comments can be initiated by using `//`
- Everything after `//` up to the end of the current line is ignored and not parsed
```
// This is a comment
```
# Feature Tracker
## High level Components ## High level Components
- [ ] Lexer: Transforms text into Tokens - [x] Lexer: Transforms text into Tokens
- [ ] Parser: Transforms Tokens into Abstract Syntax Tree - [x] Parser: Transforms Tokens into Abstract Syntax Tree
- [ ] Interpreter (tree-walk-interpreter): Walks the tree and evaluates the expressions / statements - [x] Interpreter (tree-walk-interpreter): Walks the tree and evaluates the expressions / statements
## Language features
- [x] General expressions
- [x] Arithmetic operations
- [x] Addition `a + b`
- [x] Subtraction `a - b`
- [x] Multiplication `a * b`
- [x] Division `a / b`
- [x] Modulo `a % b
- [x] Negate `-a`
- [x] Parentheses `(a + b) * c`
- [x] Logical boolean operators
- [x] Equal `a == b`
- [x] Not equal `a != b`
- [x] Greater than `a > b`
- [x] Less than `a < b`
- [x] Greater than or equal `a >= b`
- [x] Less than or equal `a <= b`
- [x] Logical operators
- [x] And `a && b`
- [x] Or `a || b`
- [x] Not `!a`
- [x] Bitwise operators
- [x] Bitwise AND `a & b`
- [x] Bitwise OR `a | b`
- [x] Bitwise XOR `a ^ b`
- [x] Bitwise NOT `~a`
- [x] Bitwise left shift `a << b`
- [x] Bitwise right shift `a >> b`
- [x] Variables
- [x] Declaration
- [x] Assignment
- [x] Statements with semicolon & Multiline programs
- [x] Control flow
- [x] While loop `while X { ... }`
- [x] If else statement `if X { ... } else { ... }`
- [x] If Statement
- [x] Else statement
- [x] Line comments `//`
- [x] Strings
- [x] IO Intrinsics
- [x] Print
## Grammar
### Expressions
```
LITERAL = I64_LITERAL | STR_LITERAL
expr_primary = LITERAL | IDENT | "(" expr ")" | "-" expr_primary | "~" expr_primary
expr_mul = expr_primary (("*" | "/" | "%") expr_primary)*
expr_add = expr_mul (("+" | "-") expr_mul)*
expr_shift = expr_add ((">>" | "<<") expr_add)*
expr_rel = expr_shift ((">" | ">=" | "<" | "<=") expr_shift)*
expr_equ = expr_rel (("==" | "!=") expr_rel)*
expr_band = expr_equ ("&" expr_equ)*
expr_bxor = expr_band ("^" expr_band)*
expr_bor = expr_bxor ("|" expr_bxor)*
expr_land = expr_bor ("&&" expr_bor)*
expr_lor = expr_land ("||" expr_land)*
expr = expr_lor
```
### Statements
```
stmt_if = "if" expr "{" stmt* "}" ("else" "{" stmt* "}")?
stmt_loop = "loop" expr (";" expr)? "{" stmt* "}"
stmt_expr = expr ";"
stmt = stmt_expr | stmt_loop
```

15
examples/euler1.nek Normal file
View File

@@ -0,0 +1,15 @@
// If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
// The sum of these multiples is 23.
// Find the sum of all the multiples of 3 or 5 below 1000.
//
// Correct Answer: 233168
sum <- 0;
i <- 0;
loop i < 1_000; i = i + 1 {
if i % 3 == 0 | i % 5 == 0 {
sum = sum + i;
}
}
print sum;

26
examples/euler2.nek Normal file
View File

@@ -0,0 +1,26 @@
// Each new term in the Fibonacci sequence is generated by adding the previous two terms.
// By starting with 1 and 2, the first 10 terms will be:
// 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
// By considering the terms in the Fibonacci sequence whose values do not exceed four million,
// find the sum of the even-valued terms.
//
// Correct Answer: 4613732
sum <- 0;
a <- 0;
b <- 1;
tmp <- 0;
loop a < 4_000_000 {
if a % 2 == 0 {
sum = sum + a;
}
tmp = a;
a = b;
b = b + tmp;
}
print sum;

29
examples/euler3.nek Normal file
View File

@@ -0,0 +1,29 @@
// The prime factors of 13195 are 5, 7, 13 and 29.
// What is the largest prime factor of the number 600851475143 ?
//
// Correct Answer: 6857
number <- 600_851_475_143;
result <- 0;
div <- 2;
loop number > 1 {
loop number % div == 0 {
if div > result {
result = div;
}
number = number / div;
}
div = div + 1;
if div * div > number {
if number > 1 & number > result {
result = number;
}
number = 0;
}
}
print result;

36
examples/euler4.nek Normal file
View File

@@ -0,0 +1,36 @@
// A palindromic number reads the same both ways. The largest palindrome made from the product of
// two 2-digit numbers is 9009 = 91 × 99.
// Find the largest palindrome made from the product of two 3-digit numbers.
//
// Correct Answer: 906609
res <- 0;
tmp <- 0;
num <- 0;
num_rev <- 0;
i <- 100;
k <- 100;
loop i < 1_000; i = i + 1 {
k = 100;
loop k < 1_000; k = k + 1 {
num_rev = 0;
num = i * k;
tmp = num;
loop tmp {
num_rev = num_rev*10 + tmp % 10;
tmp = tmp / 10;
}
if num == num_rev & num > res {
res = num;
}
}
}
print res;

24
examples/euler4.py Normal file
View File

@@ -0,0 +1,24 @@
# A palindromic number reads the same both ways. The largest palindrome made from the product of
# two 2-digit numbers is 9009 = 91 × 99.
# Find the largest palindrome made from the product of two 3-digit numbers.
#
# Correct Answer: 906609
res = 0
for i in range(100, 999):
for k in range(100, 999):
num = i * k
tmp = num
num_rev = 0
while tmp != 0:
num_rev = num_rev*10 + tmp % 10
tmp = tmp // 10
if num == num_rev and num > res:
res = num
print(res)

28
examples/euler5.nek Normal file
View File

@@ -0,0 +1,28 @@
// 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
// What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
//
// Correct Answer: 232_792_560
num <- 20;
should_continue <- 1;
i <- 2;
loop should_continue {
should_continue = 0;
i = 20;
loop i >= 2; i = i - 1 {
if num % i != 0 {
should_continue = 1;
// break
i = 0;
}
}
if should_continue == 1 {
num = num + 20;
}
}
print num;

150
src/ast.rs Normal file
View File

@@ -0,0 +1,150 @@
use std::rc::Rc;
/// Types for binary operators
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum BinOpType {
/// Addition
Add,
/// Subtraction
Sub,
/// Multiplication
Mul,
/// Divide
Div,
/// Modulo
Mod,
/// Compare Equal
EquEqu,
/// Compare Not Equal
NotEqu,
/// Less than
Less,
/// Less than or Equal
LessEqu,
/// Greater than
Greater,
/// Greater than or Equal
GreaterEqu,
/// Bitwise OR (inclusive or)
BOr,
/// Bitwise And
BAnd,
/// Bitwise Xor (exclusive or)
BXor,
/// Logical And
LAnd,
/// Logical Or
LOr,
/// Shift Left
Shl,
/// Shift Right
Shr,
/// Assign value to variable
Assign,
/// Declare new variable with value
Declare,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum UnOpType {
/// Unary Negate
Negate,
/// Bitwise Not
BNot,
/// Logical Not
LNot,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Expression {
/// Integer literal (64-bit)
I64(i64),
/// String literal
String(Rc<String>),
/// Variable
Var(String),
/// Binary operation. Consists of type, left hand side and right hand side
BinOp(BinOpType, Box<Expression>, Box<Expression>),
/// Unary operation. Consists of type and operand
UnOp(UnOpType, Box<Expression>),
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Loop {
/// The condition that determines if the loop should continue
pub condition: Expression,
/// This is executed after each loop to advance the condition variables
pub advancement: Option<Expression>,
/// The loop body that is executed each loop
pub body: Ast,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct If {
/// The condition
pub condition: Expression,
/// The body that is executed when condition is true
pub body_true: Ast,
/// The if body that is executed when the condition is false
pub body_false: Ast,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Statement {
Expr(Expression),
Loop(Loop),
If(If),
Print(Expression),
}
#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct Ast {
pub prog: Vec<Statement>,
}
impl BinOpType {
/// Get the precedence for a binary operator. Higher value means the OP is stronger binding.
/// For example Multiplication is stronger than addition, so Mul has higher precedence than Add.
///
/// The operator precedences are derived from the C language operator precedences. While not all
/// C operators are included or the exact same, the precedence oder is the same.
/// See: https://en.cppreference.com/w/c/language/operator_precedence
pub fn precedence(&self) -> u8 {
match self {
BinOpType::Declare => 0,
BinOpType::Assign => 1,
BinOpType::LOr => 2,
BinOpType::LAnd => 3,
BinOpType::BOr => 4,
BinOpType::BXor => 5,
BinOpType::BAnd => 6,
BinOpType::EquEqu | BinOpType::NotEqu => 7,
BinOpType::Less | BinOpType::LessEqu | BinOpType::Greater | BinOpType::GreaterEqu => 8,
BinOpType::Shl | BinOpType::Shr => 9,
BinOpType::Add | BinOpType::Sub => 10,
BinOpType::Mul | BinOpType::Div | BinOpType::Mod => 11,
}
}
}

View File

@@ -1,70 +1,218 @@
use crate::parser::{Ast, BinOpType}; use std::{fmt::Display, rc::Rc};
use crate::{
ast::{Ast, BinOpType, Expression, If, Statement, UnOpType},
lexer::lex,
parser::parse,
};
#[derive(Debug, PartialEq, Eq, Clone)] #[derive(Debug, PartialEq, Eq, Clone)]
pub enum Value { pub enum Value {
I64(i64), I64(i64),
String(Rc<String>),
} }
pub struct Interpreter { pub struct Interpreter {
// Runtime storage, for example variables ... // Variable table stores the runtime values of variables
vartable: Vec<(String, Value)>,
} }
impl Interpreter { impl Interpreter {
pub fn new() -> Self { pub fn new() -> Self {
Self {} Self {
} vartable: Vec::new(),
pub fn run(&mut self, prog: Ast) {
let result = self.resolve_expr(prog);
println!("Result = {:?}", result);
}
fn resolve_expr(&mut self, expr: Ast) -> Value {
match expr {
Ast::I64(val) => Value::I64(val),
Ast::BinOp(bo, lhs, rhs) => self.resolve_binop(bo, *lhs, *rhs),
} }
} }
fn resolve_binop(&mut self, bo: BinOpType, lhs: Ast, rhs: Ast) -> Value { fn get_var(&self, name: &str) -> Option<Value> {
let lhs = self.resolve_expr(lhs); self.vartable
.iter()
.rev()
.find(|it| it.0 == name)
.map(|it| it.1.clone())
}
fn get_var_mut(&mut self, name: &str) -> Option<&mut Value> {
self.vartable
.iter_mut()
.rev()
.find(|it| it.0 == name)
.map(|it| &mut it.1)
}
pub fn run_str(&mut self, code: &str, print_tokens: bool, print_ast: bool) {
let tokens = lex(code).unwrap();
if print_tokens {
println!("Tokens: {:?}", tokens);
}
let ast = parse(tokens);
if print_ast {
println!("{:#?}", ast);
}
self.run(&ast);
}
pub fn run(&mut self, prog: &Ast) {
let vartable_len = self.vartable.len();
for stmt in &prog.prog {
match stmt {
Statement::Expr(expr) => {
self.resolve_expr(expr);
}
Statement::Loop(looop) => {
// loop runs as long condition != 0
loop {
if matches!(self.resolve_expr(&looop.condition), Value::I64(0)) {
break;
}
self.run(&looop.body);
if let Some(adv) = &looop.advancement {
self.resolve_expr(&adv);
}
}
}
Statement::Print(expr) => {
let result = self.resolve_expr(expr);
print!("{}", result);
}
Statement::If(If {
condition,
body_true,
body_false,
}) => {
if matches!(self.resolve_expr(condition), Value::I64(0)) {
self.run(body_false);
} else {
self.run(body_true);
}
}
}
}
self.vartable.truncate(vartable_len);
}
fn resolve_expr(&mut self, expr: &Expression) -> Value {
match expr {
Expression::I64(val) => Value::I64(*val),
Expression::String(text) => Value::String(text.clone()),
Expression::BinOp(bo, lhs, rhs) => self.resolve_binop(bo, lhs, rhs),
Expression::UnOp(uo, operand) => self.resolve_unop(uo, operand),
Expression::Var(name) => self.resolve_var(name),
}
}
fn resolve_var(&mut self, name: &str) -> Value {
match self.get_var(name) {
Some(val) => val.clone(),
None => panic!("Variable '{}' used but not declared", name),
}
}
fn resolve_unop(&mut self, uo: &UnOpType, operand: &Expression) -> Value {
let operand = self.resolve_expr(operand);
match (operand, uo) {
(Value::I64(val), UnOpType::Negate) => Value::I64(-val),
(Value::I64(val), UnOpType::BNot) => Value::I64(!val),
(Value::I64(val), UnOpType::LNot) => Value::I64(if val == 0 { 1 } else { 0 }),
_ => panic!("Value type is not compatible with unary operation"),
}
}
fn resolve_binop(&mut self, bo: &BinOpType, lhs: &Expression, rhs: &Expression) -> Value {
let rhs = self.resolve_expr(rhs); let rhs = self.resolve_expr(rhs);
match (&bo, &lhs) {
(BinOpType::Declare, Expression::Var(name)) => {
self.vartable.push((name.clone(), rhs.clone()));
return rhs;
}
(BinOpType::Assign, Expression::Var(name)) => {
match self.get_var_mut(name) {
Some(val) => *val = rhs.clone(),
None => panic!("Runtime Error: Trying to assign value to undeclared variable"),
}
return rhs;
}
_ => (),
}
let lhs = self.resolve_expr(lhs);
match (lhs, rhs) { match (lhs, rhs) {
(Value::I64(lhs), Value::I64(rhs)) => match bo { (Value::I64(lhs), Value::I64(rhs)) => match bo {
BinOpType::Add => Value::I64(lhs + rhs), BinOpType::Add => Value::I64(lhs + rhs),
BinOpType::Mul => Value::I64(lhs * rhs), BinOpType::Mul => Value::I64(lhs * rhs),
BinOpType::Sub => Value::I64(lhs - rhs),
BinOpType::Div => Value::I64(lhs / rhs),
BinOpType::Mod => Value::I64(lhs % rhs),
BinOpType::BOr => Value::I64(lhs | rhs),
BinOpType::BAnd => Value::I64(lhs & rhs),
BinOpType::BXor => Value::I64(lhs ^ rhs),
BinOpType::LAnd => Value::I64(if (lhs != 0) && (rhs != 0) { 1 } else { 0 }),
BinOpType::LOr => Value::I64(if (lhs != 0) || (rhs != 0) { 1 } else { 0 }),
BinOpType::Shr => Value::I64(lhs >> rhs),
BinOpType::Shl => Value::I64(lhs << rhs),
BinOpType::EquEqu => Value::I64(if lhs == rhs { 1 } else { 0 }),
BinOpType::NotEqu => Value::I64(if lhs != rhs { 1 } else { 0 }),
BinOpType::Less => Value::I64(if lhs < rhs { 1 } else { 0 }),
BinOpType::LessEqu => Value::I64(if lhs <= rhs { 1 } else { 0 }),
BinOpType::Greater => Value::I64(if lhs > rhs { 1 } else { 0 }),
BinOpType::GreaterEqu => Value::I64(if lhs >= rhs { 1 } else { 0 }),
BinOpType::Declare | BinOpType::Assign => unreachable!(),
}, },
// _ => panic!("Value types are not compatible"), _ => panic!("Value types are not compatible"),
}
}
}
impl Display for Value {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Value::I64(val) => write!(f, "{}", val),
Value::String(text) => write!(f, "{}", text),
} }
} }
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::parser::{Ast, BinOpType};
use super::{Interpreter, Value}; use super::{Interpreter, Value};
use crate::ast::{BinOpType, Expression};
#[test] #[test]
fn test_interpreter_expr() { fn test_interpreter_expr() {
// Expression: 1 + 2 * 3 + 4 // Expression: 1 + 2 * 3 + 4
// With precedence: (1 + (2 * 3)) + 4 // With precedence: (1 + (2 * 3)) + 4
let ast = Ast::BinOp( let ast = Expression::BinOp(
BinOpType::Add, BinOpType::Add,
Ast::BinOp( Expression::BinOp(
BinOpType::Add, BinOpType::Add,
Ast::I64(1).into(), Expression::I64(1).into(),
Ast::BinOp(BinOpType::Mul, Ast::I64(2).into(), Ast::I64(3).into()).into(), Expression::BinOp(
BinOpType::Mul,
Expression::I64(2).into(),
Expression::I64(3).into(),
)
.into(),
) )
.into(), .into(),
Ast::I64(4).into(), Expression::I64(4).into(),
); );
let expected = Value::I64(11); let expected = Value::I64(11);
let mut interpreter = Interpreter::new(); let mut interpreter = Interpreter::new();
let actual = interpreter.resolve_expr(ast); let actual = interpreter.resolve_expr(&ast);
assert_eq!(expected, actual); assert_eq!(expected, actual);
} }

View File

@@ -1,23 +1,31 @@
use crate::token::Token;
use anyhow::Result;
use std::{iter::Peekable, str::Chars}; use std::{iter::Peekable, str::Chars};
use thiserror::Error;
use crate::parser::BinOpType; #[derive(Debug, Error)]
pub enum LexErr {
#[error("Failed to parse '{0}' as i64")]
NumericParse(String),
#[derive(Debug, PartialEq, Eq)] #[error("Invalid escape character '\\{0}'")]
pub enum Token { InvalidStrEscape(char),
/// Integer literal (64-bit)
I64(i64),
/// Plus (+) #[error("Lexer encountered unexpected char: '{0}'")]
Add, UnexpectedChar(char),
/// Asterisk (*) #[error("Missing closing string quote '\"'")]
Mul, MissingClosingString,
}
/// End of file /// Lex the provided code into a Token Buffer
EoF, pub fn lex(code: &str) -> Result<Vec<Token>, LexErr> {
let mut lexer = Lexer::new(code);
lexer.lex()
} }
struct Lexer<'a> { struct Lexer<'a> {
/// The sourcecode text as an iterator over the chars
code: Peekable<Chars<'a>>, code: Peekable<Chars<'a>>,
} }
@@ -27,65 +35,195 @@ impl<'a> Lexer<'a> {
Self { code } Self { code }
} }
fn lex(&mut self) -> Vec<Token> { fn lex(&mut self) -> Result<Vec<Token>, LexErr> {
let mut tokens = Vec::new(); let mut tokens = Vec::new();
while let Some(ch) = self.next() { loop {
match ch { match self.next() {
// Stop lexing at EOF
'\0' => break,
// Skip whitespace // Skip whitespace
' ' => (), ' ' | '\t' | '\n' | '\r' => (),
// Lex numbers // Line comment. Consume every char until linefeed (next line)
'0'..='9' => { '/' if matches!(self.peek(), '/') => while !matches!(self.next(), '\n' | '\0') {},
let mut sval = String::from(ch);
// Do as long as a next char exists and it is a numeric char // Double character tokens
while let Some('0'..='9') = self.peek() { '>' if matches!(self.peek(), '>') => {
// The next char is verified to be Some, so unwrap is safe self.next();
sval.push(self.next().unwrap()); tokens.push(Token::Shr);
} }
'<' if matches!(self.peek(), '<') => {
// TODO: We only added numeric chars to the string, but the conversion could still fail self.next();
tokens.push(Token::I64(sval.parse().unwrap())); tokens.push(Token::Shl);
}
'=' if matches!(self.peek(), '=') => {
self.next();
tokens.push(Token::EquEqu);
}
'!' if matches!(self.peek(), '=') => {
self.next();
tokens.push(Token::NotEqu);
}
'<' if matches!(self.peek(), '=') => {
self.next();
tokens.push(Token::LAngleEqu);
}
'>' if matches!(self.peek(), '=') => {
self.next();
tokens.push(Token::RAngleEqu);
}
'<' if matches!(self.peek(), '-') => {
self.next();
tokens.push(Token::LArrow);
}
'&' if matches!(self.peek(), '&') => {
self.next();
tokens.push(Token::LAnd);
}
'|' if matches!(self.peek(), '|') => {
self.next();
tokens.push(Token::LOr);
} }
// Single character tokens
';' => tokens.push(Token::Semicolon),
'+' => tokens.push(Token::Add), '+' => tokens.push(Token::Add),
'-' => tokens.push(Token::Sub),
'*' => tokens.push(Token::Mul), '*' => tokens.push(Token::Mul),
'/' => tokens.push(Token::Div),
'%' => tokens.push(Token::Mod),
'|' => tokens.push(Token::BOr),
'&' => tokens.push(Token::BAnd),
'^' => tokens.push(Token::BXor),
'(' => tokens.push(Token::LParen),
')' => tokens.push(Token::RParen),
'~' => tokens.push(Token::Tilde),
'<' => tokens.push(Token::LAngle),
'>' => tokens.push(Token::RAngle),
'=' => tokens.push(Token::Equ),
'{' => tokens.push(Token::LBraces),
'}' => tokens.push(Token::RBraces),
'!' => tokens.push(Token::LNot),
//TODO: Don't panic, keep calm // Special tokens with variable length
_ => panic!("Lexer encountered unexpected char: '{}'", ch),
// Lex multiple characters together as numbers
ch @ '0'..='9' => tokens.push(self.lex_number(ch)?),
// Lex multiple characters together as a string
'"' => tokens.push(self.lex_str()?),
// Lex multiple characters together as identifier
ch @ ('a'..='z' | 'A'..='Z' | '_') => tokens.push(self.lex_identifier(ch)?),
ch => Err(LexErr::UnexpectedChar(ch))?,
} }
} }
tokens Ok(tokens)
}
/// Lex multiple characters as a number until encountering a non numeric digit. This includes
/// the first character
fn lex_number(&mut self, first_char: char) -> Result<Token, LexErr> {
// String representation of the integer value
let mut sval = String::from(first_char);
// Do as long as a next char exists and it is a numeric char
loop {
// The next char is verified to be Some, so unwrap is safe
match self.peek() {
// Underscore is a separator, so remove it but don't add to number
'_' => {
self.next();
}
'0'..='9' => {
sval.push(self.next());
}
// Next char is not a number, so stop and finish the number token
_ => break,
}
}
// Try to convert the string representation of the value to i64
let i64val = sval.parse().map_err(|_| LexErr::NumericParse(sval))?;
Ok(Token::I64(i64val))
}
/// Lex characters as a string until encountering an unescaped closing doublequoute char '"'
fn lex_str(&mut self) -> Result<Token, LexErr> {
// Opening " was consumed in match
let mut text = String::new();
// Read all chars until encountering the closing "
loop {
match self.peek() {
'"' => break,
// If the end of file is reached while still waiting for '"', error out
'\0' => Err(LexErr::MissingClosingString)?,
_ => match self.next() {
// Backshlash indicates an escaped character
'\\' => match self.next() {
'n' => text.push('\n'),
'r' => text.push('\r'),
't' => text.push('\t'),
'\\' => text.push('\\'),
'"' => text.push('"'),
ch => Err(LexErr::InvalidStrEscape(ch))?,
},
// All other characters are simply appended to the string
ch => text.push(ch),
},
}
}
// Consume closing "
self.next();
Ok(Token::String(text))
}
/// Lex characters from the text as an identifier. This includes the first character passed in
fn lex_identifier(&mut self, first_char: char) -> Result<Token, LexErr> {
let mut ident = String::from(first_char);
// Do as long as a next char exists and it is a valid char for an identifier
loop {
match self.peek() {
// In the middle of an identifier numbers are also allowed
'a'..='z' | 'A'..='Z' | '0'..='9' | '_' => {
ident.push(self.next());
}
// Next char is not valid, so stop and finish the ident token
_ => break,
}
}
// Check for pre-defined keywords
let token = match ident.as_str() {
"loop" => Token::Loop,
"print" => Token::Print,
"if" => Token::If,
"else" => Token::Else,
// If it doesn't match a keyword, it is a normal identifier
_ => Token::Ident(ident),
};
Ok(token)
} }
/// Advance to next character and return the removed char /// Advance to next character and return the removed char
fn next(&mut self) -> Option<char> { fn next(&mut self) -> char {
self.code.next() self.code.next().unwrap_or('\0')
} }
/// Get the next character without removing it /// Get the next character without removing it
fn peek(&mut self) -> Option<char> { fn peek(&mut self) -> char {
self.code.peek().copied() self.code.peek().copied().unwrap_or('\0')
}
}
/// Lex the provided code into a Token Buffer
///
/// TODO: Don't panic and implement error handling using Result
pub fn lex(code: &str) -> Vec<Token> {
let mut lexer = Lexer::new(code);
lexer.lex()
}
impl Token {
pub fn try_to_binop(&self) -> Option<BinOpType> {
Some(match self {
Token::Add => BinOpType::Add,
Token::Mul => BinOpType::Mul,
_ => return None,
})
} }
} }
@@ -95,7 +233,7 @@ mod tests {
#[test] #[test]
fn test_lexer() { fn test_lexer() {
let code = "33 +5*2 + 4456467*2334+3"; let code = "33 +5*2 + 4456467*2334+3 % - / << ^ | & >>";
let expected = vec![ let expected = vec![
Token::I64(33), Token::I64(33),
Token::Add, Token::Add,
@@ -108,9 +246,17 @@ mod tests {
Token::I64(2334), Token::I64(2334),
Token::Add, Token::Add,
Token::I64(3), Token::I64(3),
Token::Mod,
Token::Sub,
Token::Div,
Token::Shl,
Token::BXor,
Token::BOr,
Token::BAnd,
Token::Shr,
]; ];
let actual = lex(code); let actual = lex(code).unwrap();
assert_eq!(expected, actual); assert_eq!(expected, actual);
} }
} }

View File

@@ -1,3 +1,5 @@
pub mod lexer; pub mod lexer;
pub mod token;
pub mod parser; pub mod parser;
pub mod ast;
pub mod interpreter; pub mod interpreter;

View File

@@ -1,23 +1,55 @@
use nek_lang::{lexer::lex, parser::parse, interpreter::Interpreter}; use std::{
env::args,
fs,
io::{stdin, stdout, Write},
};
use nek_lang::interpreter::Interpreter;
#[derive(Debug, Default)]
struct CliConfig {
print_tokens: bool,
print_ast: bool,
interactive: bool,
file: Option<String>,
}
fn main() { fn main() {
let mut conf = CliConfig::default();
let mut code = String::new(); // Go through all commandline arguments except the first (filename)
for arg in args().skip(1) {
std::io::stdin().read_line(&mut code).unwrap(); match arg.as_str() {
let code = code.trim(); "--token" | "-t" => conf.print_tokens = true,
"--ast" | "-a" => conf.print_ast = true,
let tokens = lex(&code); "--interactive" | "-i" => conf.interactive = true,
file if conf.file.is_none() => conf.file = Some(file.to_string()),
println!("Tokens: {:?}\n", tokens); _ => panic!("Invalid argument: '{}'", arg),
}
let ast = parse(tokens); }
println!("Ast: {:#?}\n", ast);
let mut interpreter = Interpreter::new(); let mut interpreter = Interpreter::new();
interpreter.run(ast); if let Some(file) = &conf.file {
let code = fs::read_to_string(file).expect(&format!("File not found: '{}'", file));
interpreter.run_str(&code, conf.print_tokens, conf.print_ast);
}
if conf.interactive || conf.file.is_none() {
let mut code = String::new();
loop {
print!(">> ");
stdout().flush().unwrap();
code.clear();
stdin().read_line(&mut code).unwrap();
if code.trim() == "exit" {
break;
}
interpreter.run_str(&code, conf.print_tokens, conf.print_ast);
}
}
} }

View File

@@ -1,23 +1,12 @@
use std::iter::Peekable; use std::iter::Peekable;
use crate::lexer::Token; use crate::ast::*;
use crate::token::Token;
/// Types for binary operators /// Parse the given tokens into an abstract syntax tree
#[derive(Debug, PartialEq, Eq, Clone)] pub fn parse<T: Iterator<Item = Token>, A: IntoIterator<IntoIter = T>>(tokens: A) -> Ast {
pub enum BinOpType { let mut parser = Parser::new(tokens);
/// Addition parser.parse()
Add,
/// Multiplication
Mul,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Ast {
/// Integer literal (64-bit)
I64(i64),
/// Binary operation. Consists of type, left hand side and right hand side
BinOp(BinOpType, Box<Ast>, Box<Ast>),
} }
struct Parser<T: Iterator<Item = Token>> { struct Parser<T: Iterator<Item = Token>> {
@@ -31,18 +20,151 @@ impl<T: Iterator<Item = Token>> Parser<T> {
Self { tokens } Self { tokens }
} }
/// Parse tokens into an abstract syntax tree. This will continuously parse statements until
/// encountering end-of-file or a block end '}' .
fn parse(&mut self) -> Ast { fn parse(&mut self) -> Ast {
self.parse_expr() let mut prog = Vec::new();
loop {
match self.peek() {
Token::Semicolon => {
self.next();
}
Token::EoF | Token::RBraces => break,
// By default try to lex a statement
_ => prog.push(self.parse_stmt()),
}
}
Ast { prog }
} }
fn parse_expr(&mut self) -> Ast { /// Parse a single statement from the tokens.
fn parse_stmt(&mut self) -> Statement {
match self.peek() {
Token::Loop => Statement::Loop(self.parse_loop()),
Token::Print => {
self.next();
let expr = self.parse_expr();
// After a statement, there must be a semicolon
if !matches!(self.next(), Token::Semicolon) {
panic!("Expected semicolon after statement");
}
Statement::Print(expr)
}
Token::If => Statement::If(self.parse_if()),
// If it is not a loop, try to lex as an expression
_ => {
let stmt = Statement::Expr(self.parse_expr());
// After a statement, there must be a semicolon
if !matches!(self.next(), Token::Semicolon) {
panic!("Expected semicolon after statement");
}
stmt
}
}
}
/// Parse an if statement from the tokens
fn parse_if(&mut self) -> If {
if !matches!(self.next(), Token::If) {
panic!("Error lexing if: Expected if token");
}
let condition = self.parse_expr();
if !matches!(self.next(), Token::LBraces) {
panic!("Error lexing if: Expected '{{'")
}
let body_true = self.parse();
if !matches!(self.next(), Token::RBraces) {
panic!("Error lexing if: Expected '}}'")
}
let mut body_false = Ast::default();
if matches!(self.peek(), Token::Else) {
self.next();
if !matches!(self.next(), Token::LBraces) {
panic!("Error lexing if: Expected '{{'")
}
body_false = self.parse();
if !matches!(self.next(), Token::RBraces) {
panic!("Error lexing if: Expected '}}'")
}
}
If {
condition,
body_true,
body_false,
}
}
/// Parse a loop statement from the tokens
fn parse_loop(&mut self) -> Loop {
if !matches!(self.next(), Token::Loop) {
panic!("Error lexing loop: Expected loop token");
}
let condition = self.parse_expr();
let mut advancement = None;
let body;
match self.next() {
Token::LBraces => {
body = self.parse();
}
Token::Semicolon => {
advancement = Some(self.parse_expr());
if !matches!(self.next(), Token::LBraces) {
panic!("Error lexing loop: Expected '{{'")
}
body = self.parse();
}
_ => panic!("Error lexing loop: Expected ';' or '{{'"),
}
if !matches!(self.next(), Token::RBraces) {
panic!("Error lexing loop: Expected '}}'")
}
Loop {
condition,
advancement,
body,
}
}
/// Parse a single expression from the tokens
fn parse_expr(&mut self) -> Expression {
let lhs = self.parse_primary(); let lhs = self.parse_primary();
self.parse_expr_precedence(lhs, 0) self.parse_expr_precedence(lhs, 0)
} }
/// Parse binary expressions with a precedence equal to or higher than min_prec /// Parse binary expressions with a precedence equal to or higher than min_prec
fn parse_expr_precedence(&mut self, mut lhs: Ast, min_prec: u8) -> Ast { fn parse_expr_precedence(&mut self, mut lhs: Expression, min_prec: u8) -> Expression {
while let Some(binop) = &self.peek().try_to_binop() { while let Some(binop) = &self.peek().try_to_binop() {
// Stop if the next operator has a lower binding power
if !(binop.precedence() >= min_prec) { if !(binop.precedence() >= min_prec) {
break; break;
} }
@@ -61,16 +183,52 @@ impl<T: Iterator<Item = Token>> Parser<T> {
rhs = self.parse_expr_precedence(rhs, binop.precedence() + 1); rhs = self.parse_expr_precedence(rhs, binop.precedence() + 1);
} }
lhs = Ast::BinOp(binop, lhs.into(), rhs.into()); lhs = Expression::BinOp(binop, lhs.into(), rhs.into());
} }
lhs lhs
} }
/// Parse a primary expression (for now only number) /// Parse a primary expression (for now only number)
fn parse_primary(&mut self) -> Ast { fn parse_primary(&mut self) -> Expression {
match self.next() { match self.next() {
Token::I64(val) => Ast::I64(val), // Literal i64
Token::I64(val) => Expression::I64(val),
// Literal String
Token::String(text) => Expression::String(text.into()),
Token::Ident(name) => Expression::Var(name),
// Parentheses grouping
Token::LParen => {
let inner_expr = self.parse_expr();
// Verify that there is a closing parenthesis
if !matches!(self.next(), Token::RParen) {
panic!("Error parsing primary expr: Exepected closing parenthesis ')'");
}
inner_expr
}
// Unary negation
Token::Sub => {
let operand = self.parse_primary();
Expression::UnOp(UnOpType::Negate, operand.into())
}
// Unary bitwise not (bitflip)
Token::Tilde => {
let operand = self.parse_primary();
Expression::UnOp(UnOpType::BNot, operand.into())
}
// Unary logical not
Token::LNot => {
let operand = self.parse_primary();
Expression::UnOp(UnOpType::LNot, operand.into())
}
tok => panic!("Error parsing primary expr: Unexpected Token '{:?}'", tok), tok => panic!("Error parsing primary expr: Unexpected Token '{:?}'", tok),
} }
@@ -87,26 +245,13 @@ impl<T: Iterator<Item = Token>> Parser<T> {
} }
} }
pub fn parse<T: Iterator<Item = Token>, A: IntoIterator<IntoIter = T>>(tokens: A) -> Ast {
let mut parser = Parser::new(tokens);
parser.parse()
}
impl BinOpType {
/// Get the precedence for a binary operator. Higher value means the OP is stronger binding.
/// For example Multiplication is stronger than addition, so Mul has higher precedence than Add.
fn precedence(&self) -> u8 {
match self {
BinOpType::Add => 0,
BinOpType::Mul => 1,
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{parse, Ast, BinOpType}; use super::{parse, BinOpType, Expression};
use crate::lexer::Token; use crate::{
parser::{Ast, Statement},
token::Token,
};
#[test] #[test]
fn test_parser() { fn test_parser() {
@@ -118,20 +263,30 @@ mod tests {
Token::I64(2), Token::I64(2),
Token::Mul, Token::Mul,
Token::I64(3), Token::I64(3),
Token::Add, Token::Sub,
Token::I64(4), Token::I64(4),
Token::Semicolon,
]; ];
let expected = Ast::BinOp( let expected = Statement::Expr(Expression::BinOp(
BinOpType::Add, BinOpType::Sub,
Ast::BinOp( Expression::BinOp(
BinOpType::Add, BinOpType::Add,
Ast::I64(1).into(), Expression::I64(1).into(),
Ast::BinOp(BinOpType::Mul, Ast::I64(2).into(), Ast::I64(3).into()).into(), Expression::BinOp(
BinOpType::Mul,
Expression::I64(2).into(),
Expression::I64(3).into(),
)
.into(),
) )
.into(), .into(),
Ast::I64(4).into(), Expression::I64(4).into(),
); ));
let expected = Ast {
prog: vec![expected],
};
let actual = parse(tokens); let actual = parse(tokens);
assert_eq!(expected, actual); assert_eq!(expected, actual);

146
src/token.rs Normal file
View File

@@ -0,0 +1,146 @@
use crate::ast::BinOpType;
#[derive(Debug, PartialEq, Eq)]
pub enum Token {
/// Integer literal (64-bit)
I64(i64),
/// String literal
String(String),
/// Identifier (name for variables, functions, ...)
Ident(String),
/// Loop keyword (loop)
Loop,
/// Print keyword (print)
Print,
/// If keyword (if)
If,
/// Else keyword (else)
Else,
/// Left Parenthesis ('(')
LParen,
/// Right Parenthesis (')')
RParen,
/// Left curly braces ({)
LBraces,
/// Right curly braces (})
RBraces,
/// Plus (+)
Add,
/// Minus (-)
Sub,
/// Asterisk (*)
Mul,
/// Slash (/)
Div,
/// Percent (%)
Mod,
/// Equal Equal (==)
EquEqu,
/// Exclamationmark Equal (!=)
NotEqu,
/// Pipe (|)
BOr,
/// Ampersand (&)
BAnd,
/// Circumflex (^)
BXor,
/// Logical AND (&&)
LAnd,
/// Logical OR (||)
LOr,
/// Shift Left (<<)
Shl,
/// Shift Right (>>)
Shr,
/// Tilde (~)
Tilde,
/// Logical not (!)
LNot,
/// Left angle bracket (<)
LAngle,
/// Right angle bracket (>)
RAngle,
/// Left angle bracket Equal (<=)
LAngleEqu,
/// Left angle bracket Equal (>=)
RAngleEqu,
/// Left arrow (<-)
LArrow,
/// Equal Sign (=)
Equ,
/// Semicolon (;)
Semicolon,
/// End of file
EoF,
}
impl Token {
pub fn try_to_binop(&self) -> Option<BinOpType> {
Some(match self {
Token::Add => BinOpType::Add,
Token::Sub => BinOpType::Sub,
Token::Mul => BinOpType::Mul,
Token::Div => BinOpType::Div,
Token::Mod => BinOpType::Mod,
Token::BAnd => BinOpType::BAnd,
Token::BOr => BinOpType::BOr,
Token::BXor => BinOpType::BXor,
Token::LAnd => BinOpType::LAnd,
Token::LOr => BinOpType::LOr,
Token::Shl => BinOpType::Shl,
Token::Shr => BinOpType::Shr,
Token::EquEqu => BinOpType::EquEqu,
Token::NotEqu => BinOpType::NotEqu,
Token::LAngle => BinOpType::Less,
Token::LAngleEqu => BinOpType::LessEqu,
Token::RAngle => BinOpType::Greater,
Token::RAngleEqu => BinOpType::GreaterEqu,
Token::LArrow => BinOpType::Declare,
Token::Equ => BinOpType::Assign,
_ => return None,
})
}
}