Skip to content
This repository has been archived by the owner on Jun 16, 2020. It is now read-only.
/ deci.vm Public archive

deci.vm - simple IEC61131-3 ST like-ish programming language interpreter

License

Notifications You must be signed in to change notification settings

masscry/deci.vm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

deci.vm

deci.vm - simple proof-of-concept interpreter uses IEC61131-3 ST like-ish programming language as basis for its syntax.

Dependencies

  1. C++ compiler with C++11 support
  2. CMake 3.5
  3. GNU Bison 3.0.4
  4. Flex 2.6.4

Platform

Theoreticaly, code can be compiled and run on any platform, where one can build code using modern C++11 compiler, but Windows lacks decent Flex/GNU Bison version, so no luck there. I'll include a copy of generated code to use when no Flex/GNU Bison available in some future releases.

API

Library has several distinct parts:

  • classes implementing any/variant pattern:
    • value_t - basic abstract class;
    • nothing_t - singleton class implements NULL-class patter;
    • number_t - double precision IEEE 754 number;
    • string_t - byte string;
    • array_t - sequence container, elements stored contiguously;
    • reference_t - can reference to other objects and keeps count;
    • dictionary_t - unordered associative container;
    • function_t - abstract class implements execution capabilities;
  • classes implementing virtual machine (VM):
    • stack_t - implements VM execution context, which consists of defined variables and stack itself;
    • vm_t - simple wrapper, when called tries to execute function on top of its stack;
    • program_t - class implements user-defined interpretable code;
    • binop_t - template implements basic function of two arguments;
    • unrop_t - template implements basic function of single argument;
  • standard library of functions to use with user-defined code:
    • sum_t - A + B;
    • sub_t - A - B ;
    • mul_t - A * B;
    • div_t - A / B;
    • ls_t - A < B;
    • gr_t - A > B;
    • le_t - A <= B;
    • ge_t - A >= B;
    • eq_t - A == B;
    • neq_t - A != B;
    • or_t - A || B;
    • and_t - A && B;
    • xor_t - A ^ B;
    • pow_t - pow(A, B);
    • mod_t - A%B;
    • not_t - !A;
    • neg_t - -A;
    • print_t - print to stdout;
  • ST language compiler
    • deci_scanner_t - lexer class, body of get_next_token function generated by Flex from "iec61131.l";
    • parser_t - parser class, class declaration/definition generated by GNU BISON from "iec61131.y";
    • ast_item_t - interface class implementing single AST branch/leaf, method Generate writes compiled code for program_t to interpret;
    • ast_number_t - numeric constant leaf;
    • ast_identifier_t - some text nonterminal symbol in program;
    • ast_arg_list_t - list of arguments divied by comma;
    • ast_postfix_t - postfix operation (mainly function calls);
    • ast_binary_t - infix binary operation;
    • ast_unary_t - prefix unary operation;
    • ast_return_t - operation which dumps unassigned expression results;
    • ast_set_t - operation set new variable value;
    • ast_t - sequence of ast_item_t elements;
    • ast_if_t - if/else/end_if statement;
    • ast_for_t - for/end_for statement;
    • ast_while_t - while/end_while statement;
    • ast_repeat_t - repeat/end_repeat statement;

Test Code

  1. basic_test - small obsolete code to test deci::program_t assembler
  2. values_test - small unfinished code to test deci::value_t derived classes
  3. compile_test - simple REPL iterpreter, or non-interactive interpreter of program given in test arguments list.

Build

mkdir stage
cd stage
cmake ../
cmake --build ./

Run Interpreter

./stage/compile_test ./test/prog.st

Run REPL

./stage/compile_test

>> 123 + 321;
444