Interpreter for a subset of Lisp

T K SOURAB

Interpreter?

  • Converting information in one form to another

Interpreter in Computer Science?

  • Converting information in one form to another
  • Execute it

WHY?

  • Makes your life easier

Less explicit, more generic

 

 

You don't want to repeat yourself.

Regex
DSL

Your own Language


 

PASHNA

  • It is made to interpreter a subset of programming language called Scheme.

  • Scheme is a functional programming language from Lisp family of programming languages.

POLISH NOTATION

SAY GOODBYE TO BODMAS

SCHEME

(define fac
           (lambda (n)
                   (if (= n 0)
                          1
                          (* n (fac (- n 1))))))

Factorial

(define fib
           (lambda (n)
                   (if (= n 0)
                          0
                          (if (= n 1)
                                       1
                                      (+ (fib (- n 1)) (fib (- n 2)))))))

Fibonnaci

(define ack
           (lambda (m n)
                   (if (= m 0)
                          (+ n 1)
                          (if (= n 0)
                                       (ack (- m 1) 1)
                                      (ack (- m 1) (ack m (- n 1)))))))
(define gcd
           (lambda (a b)
                   (if (= a b)
                          a
                          (if (> a b)
                                       (gcd (- a b) b)
                                      (gcd a (- b a))))))

GCD

Ackerman's function

Working of Pashna

  1. PARSING
  2. EXECUTION

PARSING

Parsing is traditionally separated into two parts: lexical analysis, in which the input character string is broken up into a sequence of tokens, and syntactic analysis, in which the tokens are assembled into an abstract syntax tree

Tokenize

Reading 

def tokenize(chars: str) -> list:
    "Convert a string of characters into a list of tokens."

    return chars.replace('(', ' ( ').replace(')', ' ) ').split()
def read_from_tokens(tokens):
    "Read an expression from a sequence of tokens."
    if len(tokens) == 0:
        raise SyntaxError('unexpected EOF while reading')
    token = tokens.pop(0)
    if '(' == token:
        L = []
        while tokens[0] != ')':
            L.append(read_from_tokens(tokens))
        tokens.pop(0) # pop off ')'
        return L
    elif ')' == token:
        raise SyntaxError('unexpected )')
    else:
        return atom(token)

EXECUTION

def eval(x, env=global_env):
    "Evaluate an expression in an environment."
    if isinstance(x, Symbol):      # variable reference
        return env.find(x)[x]
    elif not isinstance(x, List):  # constant literal
        return x                
    elif x[0] == 'quote':          # (quote exp)
        (_, exp) = x
        return exp
    elif x[0] == 'if':             # (if test conseq alt)
        (_, test, conseq, alt) = x
        exp = (conseq if eval(test, env) else alt)
        return eval(exp, env)
    elif x[0] == 'define':         # (define var exp)
        (_, var, exp) = x
        env[var] = eval(exp, env)
    elif x[0] == 'set!':           # (set! var exp)
        (_, var, exp) = x
        env.find(var)[var] = eval(exp, env)
    elif x[0] == 'lambda':         # (lambda (var...) body)
        (_, parms, body) = x
        return Procedure(parms, body, env)
    else:                          # (proc arg...)
        proc = eval(x[0], env)
        args = [eval(exp, env) for exp in x[1:]]
        return proc(*args)

DEMO

THANK YOU

Interpreter for a subset of Lisp

By tk sourabh

Interpreter for a subset of Lisp

  • 1,344