Skip to content

svan9/nan

Repository files navigation

NAN LANGUAGE

!!--WARNING--!! Now repository includes only Nan Virtual Machine. In future should includes also Nan Language Compiler for Nan Virtual Machine See library native usage

Static Badge C++ CMake TEST TEST GitHub last commit GitHub commit activity GitHub License GitHub User's stars GitHub watchers

Nan Language compiler for Nan Virtual Machine. Also includes own Nan Virtual Machine. Uses cmake for build. Binary nan (./src) - test (in the future, a complete compiler for Nan Language). Binary nanvm (./vm) - Nan Virtual Machine executable for *.nb file ak binary nvm files.

Usage

./nan
./nanvm "path/to/file"

VM uses virtual namespace. VM Lib uses virtual::lib namespace

code-docs

See /nan for in code documentation

References

Tests

  • windows - tested
  • ubuntu - not full test (errored)

lib-usage

visual.hpp

print 'hellow word' to console

#include "virtual.hpp"

int main() {
//! build simple code
	using namespace Virtual;
	CodeBuilder builder;
	// puts - print wchar_t string from data which ends with zero byte 
	builder << Instruction_PUTS;
	builder << 0U;
	// !important - exit of program, else can run data bytes
	// if used default vm flag HeapLockExecute should errored without 'EXIT'
	builder << Instruction_EXIT;
	// add data at offset +0
	builder += L"hellow word";
	// release code
	Code* code = *builder;
//! save & execute from saved file
	// save code to file
	Code_SaveFromFile(*code, "./hellow_word.nb");
	// execute from file
	Execute("./hellow_word.nb");
//! execute from runtime
	// execute from code
	Execute(*code);
//! manial execution 
	// execute with own vm
	VirtualMachine vm;
	// allocate memory needs for code
	Alloc(vm, *code);
	// load memory from code
	LoadMemory(vm, *code);
	// execute code
	Run(vm, *code);
}

for the inserted code, it is best to use 'virtualdub.hpp'. Otherwise, you will have to count bytes for jumps. The 'CodeBuilder' from 'virtual' is used only by internal build tools

virtuallib.hpp

print number from 1 to 10

#include "virtuallib.hpp"

int main() {
	using namespace Virtual;
	using namespace Virtual::Lib;
	Builder b;
	b.BeginFunction("main");
	/* TEST 'for' */
	b.BeginFunction("l1");
	/* 'for (int i = 0; i < 10; i)' */
	b.BeginForI("i", 0, 10);
	/*{*/
	/* */ b.Puti();
	/*}*/
	b.EndForI("i");
	// save for read
	b.Save("./vlib-for-10.nb");
	// build & run code
	b.Run();
}