GithubHelp home page GithubHelp logo

doytsujin / an-embedded-c-interpreter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gyumeijie/an-embedded-c-interpreter

0.0 1.0 0.0 81 KB

a very simple interpreter for c, inspired by c4, but it is embedded

Home Page: https://github.com/rswier/c4

C 95.48% C++ 3.99% Makefile 0.53%

an-embedded-c-interpreter's Introduction

introduction

This is a very simple interpreter for c-like code, inspired by c4. The key difference between c4 and this interpreter is the latter one is embedded, and by it's very name, we can use this embedded interpreter to interpret and run a c-like code sinppet which, more often than not, is used to configure the main program, and can take the form of sole xml-file or string.

components

This tiny embedded c interpreter is mainly composed of four parts:

  • dependency
  • lex
  • parser
  • executor

example

Suppose a scenario where there are two variables in main program named result and data, and we want to multiply the data by a number say 6, and store the result in the result variable. This sounds simple right? but what we do here is to write the logical code as an external configuration data not directly in the program, yes it is data not code.

In order to realize this, we need the program to have the ability to parse the configuration data with logic into some thing can be executed,say bytecode, and the bytecode can intereact with the main program by accessing the two variables,result and data. The following steps show how to do it.

  1. Initilizatize the parser and the executor
 parser_init();  
 executor_init();
  1. Add dependecy
   dep_itemsp = init_dependency_items(2);
   add_dependency_item(dep_itemsp, "data", &data, INT);
   add_dependency_item(dep_itemsp, "result", &result, INT);

Notice that the data and result are variables in the main program, both with integer type, and the two will be used in the code block.

  1. Get the code block ready
 char* src = "use{} action{result = data * 6;}";

"use{} action{result = data * 6;}" is what we call code block, which can be place in the source file or configuration file, say xml-format file. The following xml snippet is a simple demo of xml-format code block:

<code_block name="arbitary-name-you-like">
  use{} action{result = data * 6;} 
</code_block>
  1. Compile the source code
int* code = compile_src_code(dep_itemsp, src);
  1. Run the compiled byte code
run_code(code);

When excution is done, the result ,in the main program, will have a value of 6, given data is 1.

usage

  1. dowload the repository
git clone https://github.com/Gyumeijie/an-embedded-c-interpreter.git
  1. cd into the an-embedded-c-interpreter directory
cd an-embedded-c-interpreter
  1. type make command
make
  1. run the code
./main

link

There is a project named satellite-borne-device-management uses this embedded interpreter to configure the program.

todo

  • redesign the APIs for more usablity.

Mainly center on seperating dependency variable info into two parts, the first part is static decalrative info including type and name of a dependency; And the second part is about the dynamic runtime info concerning address of that dependency.

  • refactor code for more Maintainability.
  • support more grammar.
  • add safety check for accessing dependency variable(s) in code block.

an-embedded-c-interpreter's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.