Code Monkey home page Code Monkey logo

linecode / zlang-zvm Goto Github PK

View Code? Open in Web Editor NEW

This project forked from albertzheng/zlang-zvm

0.0 2.0 0.0 1.63 MB

Z-Language is a robust, flexible, fast-speed Strong Typed embedded language for C++ application. Its Compiler and Bytecode Virtual Machine are provided too.

License: Apache License 2.0

Makefile 2.18% Java 21.89% GAP 10.47% Shell 11.80% HTML 7.37% C++ 44.19% Lex 0.07% C 1.05% Batchfile 0.12% Pascal 0.06% Visual Basic 0.02% Emacs Lisp 0.65% M4 0.15%

zlang-zvm's Introduction

Z-Language语言、编译器和字节码虚拟机

该项目是本人在2000年时的一个作品:一种健壮的、高速的强类型嵌入式语言,以及提供了该语言的编译器字节码虚拟机实现

Z-Language语言简介

Z-Language语言是我在2000年创业时为当时的创业产品NetDefender-1 IDS系统(黑客入侵检测系统)特别设计的一种强类型嵌入式编程语言,用于灵活编写和扩展IDS系统的入侵检测规则。

2000年彼时的Lua语言还很简单、且是运行时动态解析的弱类型脚本语言,满足不了IDS系统对网络数据流内容进行高速检测的要求,因此我决定自己设计一种健壮的、高速的强类型嵌入式语言,其具有如下特征:

  1. Z-Language在2000年时已经采用了字节码和虚拟机技术(类似JVM),我自己设计了一套字节码和实现了字节码虚拟机,因此能够实现真正的“一次编译,跨平台高速运行”。

  2. Z-Language采用了“沙盒”安全保护技术,因此可以保护IDS系统,避免错误编写的Z-Language程序摧毁IDS系统,或者消耗掉系统资源,例如:死循环检测、内存申请限制、函数递归层数限制等技术。

  3. Z-Language提供了一套高效率的C++嵌入式调用接口,因此能够很容易地将Z-Language集成到作者的NetDefender-1 IDS系统中的任何网络协议处理层里;

  4. Z-Language借鉴了C语言的语法结构,保留了C语言的灵活性和强大功能,但是去除了C语言中的不安全因素,例如:指针、数组、显式内存申请、释放,等等。

  5. Z-Language提供了多种丰富的基本数据类型和高级数据类型,即bool、byte、sdword、udword、sqword、uqword、double、string、rope、ipaddress、pport、table等数据类型,特别其中ipaddress、pport是专门设计的网络数据类型,rope是一种高效率的串数据类型,而table是一种比数组更灵活、更安全的关联表数据类型。

  6. Z-Language支持多种高级编程语言才具有的特征,例如:变量类型cast、函数递归、for和while循环控制、引用参数、引用变量、AND和OR逻辑操作短路、C/C++风格的宏预处理,等等。

  7. Z-Language支持将变量与NetDefender-1 IDS系统中正监测的任何TCP Session进行打结,因此可以让用户在Z-Language程序中轻松地跟踪某个TCP Session的状态变化。

  8. Z-Language提供了一套简洁的用于编写、扩展系统库函数的C++接口,在Z-Language代码里能够像原生代码那样调用新扩展的系统库函数。


Z-Language语言的语法规范

Z-Language借鉴了C语言的语法结构,保留了C语言的灵活性和强大功能,但是去除了C语言中的不安全因素,例如:指针、数组、显式内存申请、释放,等等。只要你会编写C代码,你就会编写Z-Language代码

Z-Language语言的语法规范,请参见Z-Language语言语法规范白皮书


Z-Language字节码虚拟机设计

直接阅读源代码InstructionCode.hpp里的注释,以及~/zlang-zvm/zdk/zls/zvm/下的源代码。


快速体验Z-Language语言

Z-Language语言的编译器词法分析器语法分析器语义分析器字节码生成器)和字节码虚拟机采用跨平台的C++和STL库编写实现,Z-Language语言的编译器采用自上而下(top-down)的递归下降LL(*)算法,感谢ANTLR这套开源工具,使得我们可以写出优雅的lexer/parser代码。

编译构建Z-Language语言的编译器虚拟机需要采用gcc/g++-3.0.1(注:这是2000年时可用的gcc/g++最新版本),需要使用到ANTLR-2.7.1;运行操作系统环境为FreeBSD 4.4Debian 3

Z-Language语言的编译器字节码虚拟机的源代码应该是能够被移植到现在最新版本的gcc/g++CentOS操作系统的,由于我现在的精力实在有限,未能有时间去实现,欢迎对编译器技术感兴趣的同学fork本开源项目进行PR和移植。

为了方便体验Z-Language语言,我特地构建了一个开箱即用的FreeBSD 4.4 i386的vagrant box,该vagrant box已经内置了Z-Language语言的源代码和编译、运行所需的一切工具链依赖。因此最简单的体验方法就是直接使用这个vagrant box在你的Mac或PC上启动一个FreeBSD虚拟机环境。


部署一个FreeBSD 4.4 i386虚拟机环境

前提条件:假设你的macOS已经安装了,

  1. VirtualBox;
  2. vagrant 2.0.1或之前的版本(注意:不要安装vagrant 2.0.2或以上新版本,这些版本有1个OpenSSL底层依赖的issue如下)。

    /opt/vagrant/embedded/gems/2.0.3/gems/net-ssh-4.2.0/lib/net/ssh/transport/openssl.rb:112:in `ssh_do_verify': uninitialized constant OpenSSL::Digest::DSS1 (NameError)

$ git clone [email protected]:AlbertZheng/vagrant-freebsd-4.4-i386-minimal.git
$ cd vagrant-freebsd-4.4-i386-minimal
$ vagrant up

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 23 (guest) => 2223 (host) (adapter 1)
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: root
    default: SSH auth method: private key
==> default: Machine booted and ready!
Sorry, don't know how to check guest version of Virtualbox Guest Additions on this platform. Stopping installation.
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default:
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

现在你可以以root身份ssh登录到本开源项目的专用freebsd box里了:

$ vagrant ssh

体验Z-Language语言的编译器(简称zlangc)和字节码虚拟机(简称zvm

现在你可愉快地play了:

  1. zlangc(make install安装为/usr/local/bin/zlangc)去编译用Z-Language语言编写的几个测试/演示代码文件(~/zlang-zvm/zdk/zls/zlang/data目录下*.z),将会在~/zlang-zvm/zdk/zls/zlang/data下产生相应的目标文件*.zo
# cd /root/zlang-zvm/zdk/zls/zlang/data
# zlangc demo1.z
# zlangc demo2.z
# zlangc t5.z
  1. 然后用~/zlang-zvm/zdk/zls/zvm目录下的一个演示程序demo1来体验Z-Language语言字节码虚拟机是如何执行上面编译产生的3个目标文件demo1.zodemo2.zot5.zo
# cd ../../zvm
# ./demo1

在你的C++应用程序代码里使用zvm

具体如何在你的C++程序代码里嵌入和使用zvm、以及如何用C++编写自己扩展的Z-Language语言系统函数等用法,可参考~/zlang-zvm/zdk/zls/zvm/demo1.cpp代码的实现。


Z-Language编译器命令行参数

zlangc提供的命令行参数如下:

# /usr/local/bin/zlangc --help
zlangc 1.0

Usage: zlangc [OPTIONS]... [FILES]...
   -h         --help                 Print help and exit
   -V         --version              Print version and exit
   -DSTRING   --define-macro=STRING  A macro
   -ISTRING   --include-path=STRING  A include path
   -oSTRING   --object-file=STRING   Specific obejct file
              --trace-lexer          Trace lexer
              --trace-ast            Trace AST generated
              --trace-symbol-table   Trace symbol table generated
              --trace-code           Trace byte code generated

如何从头开始编译本项目的源代码

如果你想编译本项目的源代码,先vagrant ssh登录freebsd box后,步骤如下:

编译和安装antlr-2.7.1

# cd ~/zlang-zvm/antlr-2.7.1/lib/cpp
# ./configure --prefix=/usr/local/antlr-2.7.1
# make
# make install

编译和安装Z-Language语言的编译器

# cd ~/zlang-zvm/zdk
# ./configure
# ./make
# ./make install

更多

如果您已经读到这儿了,辛苦回到页首给项目点个star吧,让更多的人可以关注到它 -_-


License

Copyright (C) 2000-2018 Albert Zheng, 郑立松

The binaries and source code of this Project can be used according to the Apache License, Version 2.0.

zlang-zvm's People

Contributors

albertzheng avatar

Watchers

 avatar  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.