valhallac

Compiler for set-theoretic programming language.
git clone git://git.knutsen.co/valhallac
Log | Files | Refs | README | LICENSE

commit 2cc247137d27d4ab36719a37f2219e1858450385
parent af7eff7a7dfa76a8d905c9f2587cdf1b2a68ca74
Author: Demonstrandum <moi@knutsen.co>
Date:   Tue, 30 Jul 2019 01:09:02 +0100

Update description.

Diffstat:
MREADME.md | 46+++++++++++++++++++++++++++++-----------------
Msrc/compiler/block.rs | 4+++-
Msrc/compiler/instructions.rs | 2++
Msrc/syntax/analyser.rs | 7++++++-
4 files changed, 40 insertions(+), 19 deletions(-)

diff --git a/README.md b/README.md @@ -2,22 +2,35 @@ <img alt="Valhalla Flag" height=230 src="https://github.com/Demonstrandum/valhalla/raw/master/assets/logo.svg.png" /> </p> -# Valhalla Language +# Valhalla Programming Language -## IN DEVELOPMENT +## IN (HEAVY) DEVELOPMENT + +What's been done so far on the front-end: -What's been done so far on the front end: - [ ] Parser - - [x] Lexical analysis, full UTF-8 support, handling: identifiers, symbols, numbers, strings (utf-8 with good number of escapes), all sorts of braces for vectors, sets, grouping, etc. + - [x] Lexical analysis, full UTF-8 support, handling: identifiers, + symbols, numbers, strings (utf-8 with a good number of escapes), + all sorts of braces for: vectors, sets and grouping, etc. - [x] Infix, prefix and suffix notation. - [x] Correct parsing of precedence, arity and associativity. - [x] The base operators for the language. - - [x] Proper function calls, Currying is properly implemented for functions (Haskell-like syntax). - - [x] (Cool) error messages, with line and column number and read-out of the line. - - [ ] Macros (incl. macro definitions and macro application). + - [x] Proper function calls, Currying / partial application + of functions is properly implemented (Haskell-like functions). + - [x] Error messages, with fancy line and column number and read-out of the source line. + - [x] Constant folding optimisations on trivially deducible + numeric computations at compile time. + - [ ] Macros (including macro definitions and macro application). + - [ ] User-defined binary operators as aliases to functions. - [ ] Compiler (generating bytecode to assemble an executable file). - [x] Table of constants and locals with basic PUSH & POP instructions as well as basic arithmetic. + - [x] Access, assignment and retrieval of local variables within + code-block scope. + - [ ] Track variable and function types. + - [ ] Marshaling, i.e. serialising the bytecode and storing it in a file + for future interpretation and execution by the virtual machine. + - [ ] ... The VM, i.e. the backend for the language, is being developed independently and will have its own progress and check-list updates. @@ -25,22 +38,21 @@ and will have its own progress and check-list updates. ### Description This repository contains the front-end (parser and -bytecode compilation) which understands the syntax and -semantics, as well as doing static type analysis and code -optimisation. The generated AST is then compiled to -Brokkr bytecode. +bytecode compilation) which processes the syntax and +semantics of the source code. The generated AST is then +compiled to [Brokkr VM](https://github.com/Demonstrandum/brokkr) bytecode. The execution of the subsequent bytecode is handled by the language's VM (virtual machine) called -Brokkr, which exists separately. +Brokkr, which exists separately from this repository. -Valhalla is a set theoretic programming language. +Valhalla is a set-theoretic programming language. That's to say, it's based on principles from set theory, in a way that all types are just sets, and hence everything is just an element of a set. The language is meant to give a -new way to think about types, and provides an intuitive way to -think about types. It may also be used to verify proofs and such -about set theory. +new way to interact with types, and provides an intuitive way to +think about them. A goal is that it may also be used to +verify proofs and such in and around set theory. -The language is a general purpose, but instead of being all OOP, +The language is a general purpose, but instead of being totally object-oriented, or functional, etc., it's just set theory based. From what I've gathered, it's not a very popular paradigm. diff --git a/src/compiler/block.rs b/src/compiler/block.rs @@ -115,14 +115,16 @@ impl<'a> LocalBlock<'a> { // Check for fast internal binary operations such as +, -, *, /, etc. let maybe_op = internal_functions::get_internal_op(&ident.value, Some(&args)); if let Some(op) = maybe_op { - self.emit(args[0]); self.emit(args[1]); + self.emit(args[0]); self.instructions.push(op); return; } } self.emit(&call_node.operands[0]); self.emit(&*call_node.callee); + self.instructions.push(Instr::Operator(Operators::CALL_N as u8)); + self.instructions.push(Instr::Operand(2)); }, _ => () }; diff --git a/src/compiler/instructions.rs b/src/compiler/instructions.rs @@ -39,6 +39,7 @@ pub enum Operators { DUP = 6, DUP_N = 7, SWAP = 8, + CALL_N = 9, N_ADD = 40, I_ADD = 41, @@ -76,6 +77,7 @@ impl fmt::Display for Operators { Operators::DUP => "DUP\n", Operators::DUP_N => "DUP_N", Operators::SWAP => "SWAP\n", + Operators::CALL_N => "CALL_N", Operators::N_ADD => "N_ADD\n", Operators::I_ADD => "I_ADD\n", diff --git a/src/syntax/analyser.rs b/src/syntax/analyser.rs @@ -53,7 +53,12 @@ fn constant_fold(node : &ast::Nodes) -> Option<ast::Nodes> { "+" => l_value + r_value, "-" => l_value - r_value, "*" => l_value * r_value, - "/" => l_value / r_value, + "/" => { + if r_value == ast::Numerics::Natural(0) { + return None; + } + l_value / r_value + }, _ => return None }; return Some(ast::Nodes::Num(ast::NumNode { value }));