Git Product home page Git Product logo

basic-interpreter's Introduction

Basic Interpreter

A trivial interpreter implemented with AST and recursive descent. The repo is no longer maintained.

Installation

  • Build within Qt

Statements, Expressions and Commands

Check the documentation for version 1 and document for version 2 for valid statements, expressions and commands.

Behaviour Specification (IMPORTANT)

The documentation describes the fundamental behaviours of the interpreter, but lots of details are neglected. Also, I've made a bit of modification to the interface to make it more usable.

  • UI modification: A text browser named “报错、提示信息” is added to display error message and extra information, such as message of successful execution.
  • Context system: In my implementation of Basic interpreter, there's no program-specific context. That is to say, all variables are defined in a global context, so it's perfectly fine if you define x in a program and use it in another. Only by runnning the CLEAR command can you clear all variables in the gloabal context.
  • Instruction input: Only five statements: INPUT, INPUTS, LETPRINT and PRINTF can be entered without a preceding line number when entered from the console, not from a file. The rest of the statements require a line number at the beginning, otherwise they cannot be stored in “代码” section.
  • Syntax tree visualization: The syntax tree is generated when executing the RUN command. All of the statements that is stored in “代码” will be scanned, parsed and visualized in “语句与语法树” section. On encountering any error, an error message will replace the syntax tree, pointing out what exactly is the problem.
  • Division: All numbers are treated as integers, so division works like that in C++, where 3/5 produces 0.
  • Variable naming: Variables must start with a letter or underscore.
  • Variables display: Variables are displayed in the order of assignment.
  • Line number range: Line number falls in the interval of $[1, 1000000]$. Preceding $0$s are allowed.
  • Negative sign: - doesn't have to be nested in a pair of parentheses. For example, 3 + -5 yields -2, not an error.
  • Code deletion: To delete a line of code, input the line number in console, without any following statement. Note that the line number must exist.

Error Handling

This section lists all kinds of errors that the interpreter can detect in each stage of execution.

Scanning

  • Detect invalid character. Valid characters include letters (upper and lower case), mathmatical operands (+ - * / < > =), underscore, parenthesis, space, \n, \t, \r, \n.The only exception is the REM statement, where you can input anything as its content.

Parsing

  • Detect syntax error.

Interpreting

  • Detect undefined variable.
  • Detect invalid jump destination.
  • Detect division by zero.
  • Detect invalid input in INPUT and INPUTS statement.
  • Detect mismatch mismatching arguments in PRINTF
  • Detect multiple statements in a line (which is not allowed in my implementation).
  • Detect power(0, 0). By mathmatical definitions, power(0, 0) invalid.

Design notes

Algorithm to build AST

I use recursive descent parsing to build the syntax tree. The grammar is as follows.

expression      ->  addition
addition        ->  multiplication (("+" | "-") multiplication)*
multiplication  ->  power (("*" | "/" power)*
unaryPower      ->  ("-" unaryPower) | power
power           ->  unary ("**" power)?
unary           ->  (("-") unary) | primary
primary         ->  NUMBER | STRING | ("("expression")")

Code details

  • Singleton pattern: make Basic and MainWindow accessible to all files, avoiding useage of extern keyword and slow signal mechanism.
  • shared_ptr eliminates any memory leak.

basic-interpreter's People

Contributors

gun9nir avatar jolynefr avatar xx01cyx avatar

Watchers

 avatar

Forkers

windowsxp-beta

basic-interpreter's Issues

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.