language

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 27560f70745d1a3e1f7f81044b1876e5c965dc00
parent a7bcee462e414264445225e2d07d4e3f696fa0f3
Author: Paul Longtine <paullongtine@gmail.com>
Date:   Tue Dec  1 08:02:24 2015

Created testbed for VM, elaborated more details in the docs, added README.md

Diffstat:
 .gitignore        |   2 +-
 README.md         |   5 +++-
 doc/OVERVIEW      |   6 +--
 doc/SPECIFICATION | 101 ++++++++++++++++++++++++++++++++++++++++++++++---------
 src/vm/Makefile   |  22 ++++++++++++-
 src/vm/inc/fh.h   |  27 +++++++++++++++-
 src/vm/inc/is.h   |   0
 src/vm/main       | Bin 0 -> 9114 bytes
 src/vm/src/fh.c   |  63 ++++++++++++++++++++++++++++++++++-
 src/vm/src/is.c   |   1 +-
 src/vm/src/main.c |  25 ++++++++++++++-
 src/vm/test       |   1 +-
 12 files changed, 235 insertions(+), 18 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1 +1,3 @@ .*.swp +*.o + diff --git a/README.md b/README.md @@ -0,0 +1,5 @@ +#'language' + +This [repository](http://github.com/bannana/language) contains the design and im +plementation of a concept interpreted language created by yours truly. Takes ins +piriation from python. diff --git a/doc/OVERVIEW b/doc/OVERVIEW @@ -58,15 +58,15 @@ PLAN +-----------------+-----------------------------------+------+ |1st week: | Finish this document. |(*) | +-----------------+-----------------------------------+------+ - |2nd week: | Outline SPECIFICATION document |( ) | + |2nd-3rd week: | Outline SPECIFICATION document |(*) | +-----------------+-----------------------------------+------+ - |3rd-6th week: | Finish VM/rudimentary interpreter |( ) | + |4rd-6th week: | Finish VM |( ) | +-----------------+-----------------------------------+------+ |7th-13th week: | Finish interpreter |( ) | +-----------------+-----------------------------------+------+ |13th-17th week: | Start work on stdlib/other libs |( ) | +-----------------+-----------------------------------+------+ - + CONCLUSION In conclusion, this project will be for self-growth and strictly selifsh diff --git a/doc/SPECIFICATION b/doc/SPECIFICATION @@ -22,15 +22,39 @@ are deemed inferior. RUNTIME + Code in the runtime is organized on a per-function basis. + +'Function' in this case means a callable block of code which may or may not retu +rn a value. + + +---------+ + |Function | -> Function is 'defined', when called with reference a new scope is + +---------+ formed, along with arguements being supplied. Can support object + oriented behaviour with preserving scopes, and providing access + methods to member functions. + + Variables (and functions) exist under various scopes. The scopes are as +follows: + * Temporary Scope + * Local Scope + * Global Scope + + Temporary scope variables are variables declared outside the Root method. +The Root method is the code that is ran any time a 'module' of code is evaluated +and/or imported. A 'module' is any file containing code. Local Scope variables +are variables in which are visible to the module, and only the module. Global +scope variables are visible anywhere within the runtime. + + A stack is used to keep a local group of values to manipulate. Values are +pushed on to the stack and are operated upon, and the values in the stack can +but assigned or re-assigned to variables. + + Bytecode Instructions + Keywords: TOS - 'Top Of Stack' The top element ------------------------------------------------------------------------------- Stack manipulation - - The 'stack' is the tool used to preform operations. Values are loaded on to -the stack and are operated upon, and the configuration of elements on the stack -is critical to the function of this machine. These instructions provide methods -to manipulate the arragement, composition, and handling of the stack. ------------------------------------------------------------------------------- POP <n> - pops the stack n times. ROT - rotates top of stack @@ -38,28 +62,65 @@ to manipulate the arragement, composition, and handling of the stack. DUP - duplicates the top of the stack ------------------------------------------------------------------------------- Variable management - - Variables are a critical part of any machine that deals with computation. -In this architecture, variables will exist as references. References will point -to a location in a data structure in the runtime. Some references will have -different data structures depending on whether a variable is declared as a -persistent, persistent local, or temporary. Persistent variables will be seen -in any scope at runtime, while persistent local variables are only visable in -the scope it was declared in, but will not be discarded after exiting the scope ------------------------------------------------------------------------------- DEC <type> <ref> - declare variable of type LOV <ref> - loads reference variable on to stack STV <ref> - stores TOS to reference variable LOC <ref> - loads constant ------------------------------------------------------------------------------- -Arithmetic Operations +Type management + + Types are in the air at this moment. I'll detail what types there are when +the time comes +------------------------------------------------------------------------------- + TYPEOF <ref> - returns type structure on TOS (used for comparing types) + CAST <ref> <type> - Tries to cast <ref> to <type> +------------------------------------------------------------------------------- +Arithmetic + OPS take the two top elements of the stack, preform an operation and push +the result on the stack. ------------------------------------------------------------------------------- ADD - adds the two top elements of the stack and pushes the result SUB - subtracts the two top elements of the stack and pushes the result MULT - multiplies the two top elements of the stack and pushes the result DIV - divides the two top elements of the stack and pushes the result ------------------------------------------------------------------------------- +Conditional Expressions + Things for booleans, < > = ! and so on and so forth. +Behaves like Arithmetic instructions +------------------------------------------------------------------------------- + GTHAN - Greather than + LTHAN - Less than + EQ - Equal to + NOT - Not this. +------------------------------------------------------------------------------- +Loops +------------------------------------------------------------------------------- + STARTL - Start of loop + CLOOP - Conditional loop. If TOS is true, continue looping, else break + BREAK - Breaks out of loop + DONE - End of loop +------------------------------------------------------------------------------- +Code flow + + These instructions dictate code flow. +------------------------------------------------------------------------------- + GOTO <addr> - Goes to address + JUMPF <n> - Goes forward <n> lines + CALL <ref> - Calls function, pushes return value on to STACK. Expects array + on TOS to be used for arguements +------------------------------------------------------------------------------- +Manipulating High-order Object Variables + + For arrays, key-value arrays, etc +------------------------------------------------------------------------------- + PUSH <ref> - Push TOS on to <ref>, expects <ref> to be an object + that supports the scheme. + AT <ref> - Push element in structure at TOS (assumes it's an index of + sorts) for <ref> object. Assumes <ref> supports the scheme. + DEL <ref> <index> - Delete element in structure at TOS (assumes it's an index + of sorts) for <ref> object. LEXICAL ANALYSIS @@ -74,7 +135,8 @@ abstract notation for the code will be broken down into a binary tree as so: <node> can be an argument of its parent node, or the next instruction. Instruction nodes are nodes that will produce an instruction, or multiple based -on the bytecode interpretation of its function. For example, this line of code: +on the bytecode interpretation of its instruction. For example, this line of +code: int x = 3 @@ -93,6 +155,9 @@ on the bytecode interpretation of its function. For example, this line of code: / \ null 3 + Functions are expressed as individual binary trees. The root of any file is +treated as an individual binary tree, as this is also a function. + The various instruction nodes are as follows: * def <type> <name> @@ -100,3 +165,9 @@ on the bytecode interpretation of its function. For example, this line of code: - See the 'TYPES' section under 'OVERVIEW' * set <name> <value> - Set a named space in memory with value specified + + Going from Binary Trees to Bytecode + + The various instruction nodes within the tree will call specific functions +that will take arguemets specified and lookahead and lookbehind to formulate the +correct bytecode equivilent. diff --git a/src/vm/Makefile b/src/vm/Makefile @@ -0,0 +1,22 @@ +SRC_DIR = src +INC_DIR = inc + +CC = gcc +CFLAGS = -Wall -I$(INC_DIR) + +DEPS = $(INC_DIR)/fh.h + +OBJ = $(SRC_DIR)/main.o \ + $(SRC_DIR)/fh.o + +OUT = main + +%.o: %.c $(DEPS) + $(CC) $(CFLAGS) -c -o $@ $< + +$(OUT): $(OBJ) + $(CC) $(CFLAGS) -o $@ $^ + +clean: + rm $(SRC_DIR)/*.o + rm $(OUT) diff --git a/src/vm/inc/fh.h b/src/vm/inc/fh.h @@ -0,0 +1,27 @@ +#ifndef FH_H +#define FH_H + +#include <stdlib.h> +#include <stdio.h> + +typedef struct func { + char op; + char* args; + struct func* next; +} func; + +func* at(func*, int); + +void push(func**, func*); + +void pop(func**); + +void del(func*); + +/*Takes a FILE arguement, reads, returns size of file. + Useful \w loops. */ +long read_size(FILE**, char*); + +int init(FILE**); + +#endif diff --git a/src/vm/inc/is.h b/src/vm/inc/is.h diff --git a/src/vm/main b/src/vm/main Binary files differ diff --git a/src/vm/src/fh.c b/src/vm/src/fh.c @@ -0,0 +1,63 @@ +#include <stdlib.h> +#include <stdio.h> + +#include "fh.h" + +func* at(func* root, int x) +{ + x--; + + if (root->next == NULL) + { + return root; + } + else + { + return at(root->next, x); + } +} + +void push(func** root, func* new_node) +{ + new_node->next = *root; + *root = new_node; +} + +void pop(func** root) +{ + if ((*root)->next != NULL) + { + func *old_node = (*root)->next; + free(*root); + *root = old_node; + } +} + +void del(func* root) +{ + if (root->next != NULL) + { + del(root->next); + } + + free(root); +} + +long read_size(FILE** f, char* fname) +{ + *f = fopen(fname, "rb"); + fseek(*f, 0, SEEK_END); + + long fsize = 0; + fsize = ftell(*f); + rewind(*f); + + return fsize; +} + +int init(FILE** f) +{ + /* initialize datastructures for instructionstuffs, + begin to read file byte-by-byte */ + return 0; +} diff --git a/src/vm/src/is.c b/src/vm/src/is.c @@ -0,0 +1 @@ + diff --git a/src/vm/src/main.c b/src/vm/src/main.c @@ -0,0 +1,25 @@ +#include <stdio.h> + +#include "fh.h" + +int main(int argc, char** argv) +{ + if (argc < 2) return -1; + + /* START TESTING STUFF */ + FILE* f; + long i = 0; + long fsize = read_size(&f, argv[1]); + + while (i<fsize) + { + int test = fgetc(f); + i++; + printf("%x\n", (unsigned)test); + } + + fclose(f); + /* END TESTING STUFF */ + + return 0; +} diff --git a/src/vm/test b/src/vm/test @@ -0,0 +1 @@ +loliyeateawtawet