Giter Club home page Giter Club logo

calc's Introduction

calc

In this language there are only expressions to compute integers or strings. An expression can be type-checked using infer to determine its type or the line of a type error. A well-formed expression can be simplified using eval to produce its unique normal form which always exists. Based on the first example from the book Practical Foundations for Programming Languages.

Language / Implementation

T = Integer | String
E = integer
  | string
  | variable
  | let variable = E in E
  | E + E
  | E - E
  | E * E
  | E ++ E
  | len(E)
  | decimal(E)
  | (E)

parse : Text -> E || Error
infer : E -> T || Error
eval  : E -> E

Usage

./calc
let x = 2 in x + x
<ctrl+D>
4 : Integer

If file prog.x contains the expression

let s1 = "fruit" in
let s2 = "cake" in
s1 ++ " " ++ s2

then you can feed it directly to the interpreter with

./calc prog.x
"fruit cake" : String

Type Checking / Inferencing

Not all syntactically valid expressions make sense.

./calc
let s1 = "fruit" in
let s2 = "cake" in
s1 ++ " " ++ len(s2)
<ctrl+D>
calc: line 3: right operand of ++ not a string

Compiler

Output C source code.

./calc -c prox.x | gcc -x c -
./a.out
fruit cake

calc's People

Contributors

evanrinehart avatar

Watchers

 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.