Giter Club home page Giter Club logo

genshi-basic's Introduction

Genshi-BASIC

Interpreter for Genshi BASIC; A simple dialect of BASIC created to learn the basics of making a programming language.

Even though writing an interpreter for a custom BASIC dialect is pretty useless, the same lexing, parsing, and interpreting fundamentals I learned can be applied to other languages.

I originally wrote this in 2019, but decided that this year (2021) I would rewrite it to practice concepts I had learned since the original implementation.

I did get kind of lazy and skipped out on implementing a REPL, so for right now it justs interprets an array of strings or a source file.

Genshi BASIC

Genshi BASIC has 41 keywords. As stated above, the grammar is based on BASIC version 2.

Genshi (ๅŽŸๅง‹) means "simple" in romaji. I chose this name because I stripped out a lot of functionality to make a simpler BASIC. Arguably this makes it pretty useless, but this is a learning project rather than a useful one.

Its worth noting that this is not fully tested. I more or less got a couple programs working correctly and called it a day. As I said before, this is a rewrite and I'm honestly very bored of it now.

Notable differences from BASIC version 2

Genshi BASIC is meant to be a simpler version of BASIC version 2. I stripped out a bunch of stuff and didn't really add anything special.

I used this page on Commodore 64 commands as reference for BASIC version 2.

Removed:

  • function declarations - DEF, FN
  • memory operations - FRE, PEEK, POKE, WAIT, NEW
  • system operations - GET, STATUS, SYS, USR, TIME
  • file operations - OPEN, LOAD, CLOSE, VERIFY, SAVE
  • "interactive" operations - RUN, STOP, LIST

Changed:

  • string variables are no longer required to end with '$'
  • removed '$' characters from CHR$,LEFT$,MID$,RIGHT$ operations

Added:

  • new keywords - XOR
  • I lost motivation to add anything else cool

Examples

# Usage of genshi BASIC interpreter

from genshibasic.interpreter import Interpeter

pgm = [
  '10 REM Example 1',
  '20 LET A=3+4 * 6/2',
  '30 PRINT "A="; A',
  '40 A=8',
  '50 PRINT "NOW A="; A',
  '100 END'
]

genshi_int = Interpeter()
genshi_int.interpret(pgm)
    
# A=15.0
# NOW A=8

Development

  • Install deps - pip3 install -r requirements.txt
  • Run tests - python3 -m unittest discover

References

genshi-basic's People

Contributors

barrettotte avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

genshi-basic's Issues

Statement parsing

  • Arguments
  • Built-in functions
  • Array Declaration
  • Function Declaration
  • Variable Declaration
  • For statement
  • Function statement
  • Go statement
  • If statement
  • Print statement

GenshiBASIC debug mode

Optional param debug=True; returns dictionary of raw input, lexemes, tokens, parse_tree, and interpretation info

Interpretation of expressions

  • Binary expressions X+3
  • Unary expressions -X
  • Grouping expressions -(4+X)
  • Literal expressisons 4
  • Function expressions SIN(X) and MYFUNC(X,Y,Z)

Improve string literal lexing

Lex literals in quotes as 'STRING' and remove quotation nodes before reaching the parser
(Since lexer will throw uneven quotation marks exception)

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.