Giter Site home page Giter Site logo

serojik91 / my_basic Goto Github PK

View Code? Open in Web Editor NEW

This project forked from paladin-t/my_basic

0.0 2.0 0.0 28.53 MB

Lightweight BASIC interpreter written with C from scratch. Aimed to be embeddable, extendable and portable, with retro and modern syntax. May be used as an alternative of something like Lua.

Home Page: http://paladin-t.github.io/my_basic/

License: MIT License

Batchfile 0.09% C 98.89% HTML 0.92% Makefile 0.10%

my_basic's Introduction

 _____ __ __     _____ _____ _____ _____ _____ 
|     |  |  |___| __  |  _  |   __|     |     |
| | | |_   _|___| __ -|     |__   |-   -|   --|
|_|_|_| |_|     |_____|__|__|_____|_____|_____|

Copyright (C) 2011 - 2017 Wang Renxin. All rights reserved.

Build status MIT license

简体中文

开发日志

Contents

Introduction

MY-BASIC is a lightweight cross-platform easy extendable BASIC interpreter written in pure C with about twenty thousand lines of source code. MY-BASIC is a dynamic typed programming language. It supports structured grammar, and implements a style of OOP called prototype-based programming paradigm, furthermore it offers a functional programming ability with lambda abstraction. It is aimed to be either an embeddable scripting language or a standalone interpreter. The core is very lightweight; all in a C source file and an associated header file; simpleness of source file layout and tightness dependency make it extraordinarily tough. Anyone even C programming newbies could learn how to use it and add new scripting interfaces in five minutes. It's able to easily combine MY-BASIC with an existing project in C, C++, Java, Objective-C, Swift, C# and many other languages. Script driven can make your projects more powerful, elegant and neat. It's also possible to learn how to build an interpreter from scratch with MY-BASIC, or build your own dialect easily based on it.

The prefix "MY-" in the name means either literally "My" or "Make Your", it's up to you.

Compatibility

It fits well on a large scale of Workstation, PC, Tablet, Pad, Mobile Phone, PDA, Video Game Console, Raspberry Pi, Arduino and even MCU; totally portable to Windows, macOS, Unix, Linux, iOS, Android, RTOS, etc.

May be used as an alternative of something like Lua.

For Arduino

There is an Arduino porting of MY-BASIC interpreter, with a totally rewritten shell and a user manual. See MY-BASIC ARDU for details.

Main features

MY-BASIC is a dynamic typed programming language with BASIC syntax and has a very dynamic nature; it makes it flexible and easy to use. MY-BASIC offers a wide range of features including:

  • It is totally free to use MY-BASIC for commercial or noncommercial purpose under the MIT license
  • Written in clean ANSI C, source code is portable to a dozen of platforms
  • Lightweight (within memory usage less than 128KB), fast, and cuttable
  • With both retro and modern BASIC syntax
  • Case-insensitive tokenization, and many other indelible BASIC feelings
  • Unicode support
  • Prototype-based programming (OOP) paradigm, with reflection support
  • Lambda abstraction enhanced functional programming
  • Customizable referenced usertype support
  • Collection construction and manipulation functions for LIST and DICT
  • Automatic releasing of referenced objects (prototype, lambda, referenced usertype, list, dictionary, etc.) benefited from Reference Counting and Garbage Collection
  • Dynamic typed integer, real, string, boolean, usertype, and other advanced types etc. with array support
  • Standard numeric functions, and standard string functions
  • Multiple source file support by IMPORT statement
  • Structured user customizable sub routine definition by DEF/ENDDEF support, with tail recursion optimization
  • Structured IF/THEN/ELSEIF/ELSE/ENDIF support
  • Structured FOR/TO/STEP/NEXT, FOR/IN/NEXT, WHILE/WEND, DO/UNTIL support
  • Reserved retro GOTO, GOSUB/RETURN support
  • Debug APIs
  • Customizable memory pool
  • High expansibility, easy to use APIs, easy to write customized scripting interfaces
  • Powerful interactive ability to manipulate script facilities at native side; or to use native functionalities in script, and vice versa
  • More features under development

You may wondering if it's possible to introduce another feature to MY-BASIC, then take a look at this page. I also write some of my plans on the language design page.

Script at a glance

Come along with a usual "Hello World" code in MY-BASIC:

input "What is your name: ", n$

def greeting(a, b)
	return a + " " + b + " by " + n$ + "."
enddef

print greeting("Hello", "world");

Read the MY-BASIC Quick Reference (especially the "Programming with BASIC" section) to get more details about how to program in MY-BASIC.

Showcase

See the list for some user creations.

Don't forget to share with us about your creativity!

Installation

Use standalone interpreter binary

This repository contains precompiled binaries for Windows and macOS, it's easy to download one of them and have a first impressive playground. Or you could make a build as follow:

  • Open the Visual Studio solution my_basic.sln on Windows to build an executable
  • Open the Xcode solution my_basic_mac.xcodeproj on macOS to build a macOS executable
  • If you were using other *nix OS, then use the makefile with a "make" toolchain to build an interpreter binary according to your specific platform

To compile an interpreter binary for your own platform manually, please follow these steps:

  1. Retrieve everything under core and shell folders for a minimum build
  2. Setup your compile toolchain configuration
  3. Compile core/my_basic.c and shell/main.c, they both require core/my_basic.h; then link up an executable

The standalone interpreter supports three running modes:

  • Execute the binary without arguments to enter the interactive mode of MY-BASIC
  • Pass a file path to the binary to load and run that script file directly
  • Pass an argument -e followed by an expression to evaluate it and print the result, as a simple calculator, eg. -e "2 * (3 + 4)"

Type "HELP" and hint Enter under interactive mode to view full detail usages of the interpreter.

Combine with existing projects

MY-BASIC is cleanly written in a single C source file and an associated header file. Just copy core/my_basic.c and core/my_basic.h to your project folder and add them to a build configuration.

You can definitely link with MY-BASIC as a lib as well.

For more details about using MY-BASIC when it has been integrated with a project, please see MY-BASIC Quick Reference or read the Wiki pages.

It's necessary to know some principle of MY-BASIC before doing deep customization; nothing's better than a workflow diagram to get a first image.

A simplest setup as follow:

int main() {
	struct mb_interpreter_t* bas = NULL;

	mb_init();
	mb_open(&bas);
	mb_load_string(bas, "print 22 / 7;", true);
	mb_run(bas, true);
	mb_close(&bas);
	mb_dispose();

	return 0;
}

Some other details are issued on the Wiki pages below.

The MY-BASIC Quick Reference includes most of the fundamental topics, however, it hasn't covered everything, such as the design principle, machinism behind MY-BASIC, effective practice, etc; these topics are issued on the Wiki:

References

Support MY-BASIC development/List of donors

I need your supports to keep this project alive. Consider supporting MY-BASIC development with a donation, if it's useful for you.

One-off donation via PayPal.

List of donors.

my_basic's People

Contributors

paladin-t avatar wangrenxin avatar wwiv 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.