A Metacircular Interpreter in Rhombus

spdegabrielle1 pts1 comments

rhombus/rhombus/rhombus/tests/example-interp.rhm at master · racket/rhombus · GitHub

//blob/show" data-turbo-transient="true" />

Skip to content

Search or jump to...

Search code, repositories, users, issues, pull requests...

-->

Search

Clear

Search syntax tips

Provide feedback

--><br>We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Cancel

Submit feedback

Saved searches

Use saved searches to filter your results more quickly

-->

Name

Query

To see all available qualifiers, see our documentation.

Cancel

Create saved search

Sign in

//blob/show;ref_cta:Sign up;ref_loc:header logged out"}"<br>Sign up

Appearance settings

Resetting focus

You signed in with another tab or window. Reload to refresh your session.<br>You signed out in another tab or window. Reload to refresh your session.<br>You switched accounts on another tab or window. Reload to refresh your session.

Dismiss alert

{{ message }}

racket

rhombus

Public

Uh oh!

There was an error while loading. Please reload this page.

Notifications<br>You must be signed in to change notification settings

Fork<br>72

Star<br>536

FilesExpand file tree

master

/example-interp.rhm

Copy path

Blame<br>More file actions

Blame<br>More file actions

Latest commit

History<br>History<br>History

63 lines (59 loc) · 1.78 KB

master

/example-interp.rhm

Copy path

Top

File metadata and controls<br>Code

Blame

63 lines (59 loc) · 1.78 KB

Raw<br>Copy raw file<br>Download raw file

Open symbols panelEdit and raw actions

10<br>11<br>12<br>13<br>14<br>15<br>16<br>17<br>18<br>19<br>20<br>21<br>22<br>23<br>24<br>25<br>26<br>27<br>28<br>29<br>30<br>31<br>32<br>33<br>34<br>35<br>36<br>37<br>38<br>39<br>40<br>41<br>42<br>43<br>44<br>45<br>46<br>47<br>48<br>49<br>50<br>51<br>52<br>53<br>54<br>55<br>56<br>57<br>58<br>59<br>60<br>61<br>62<br>63

#lang rhombus/static

// Meta-circular interpreter for a Rhombus-like variant

// of the λ-calculus with integers and lists

//

// = fun (, ...):

// | (, ...)

// |

// | ()

// |

// | [, ...]

// |

// | block: let = ;

//

// with primitive bindings cons, first, rest, +, -, *

//

// There's no precedence for operators, though; all assumed

// at the same precedence and associating to the left

// Example use of the interpreter

module main:

interp('block:

let swap = fun (x): [first(rest(x)), first(x)]

first(swap([1, 2])) + 3',

init_env)

fun interp(e, env :~ Map):

match e

// First three rules are λ-calculus (with multiple arguments)

| 'fun ($(x :: Identifier), ...): $e':

fun (arg, ...):

interp(e, env ++ {x.unwrap(): arg, ...})

| '$rator($rand, ...)':

(interp(rator, env))(interp(rand, env), ...)

| '$(x :: Identifier)':

env[x.unwrap()]

// Allow extra parens

| '($e)':

interp(e, env)

// Ints

| '$(x :: Int)':

x.unwrap()

// Lists

| '[$x, ...]':

[interp(x, env), ...]

// Operators (no attempt at precedence)

| '$x ... $(op :: Operator) $y ...':

env[op.unwrap_op()](interp('$x ...', env),

interp('$y ...', env))

// `let` as a convenience

| 'block:

let $(x :: Identifier) = $rhs ...

$body':

interp('(fun ($x): $body)($rhs ...)', env)

// Some primitives in an initial environment

def init_env:

{'cons'.unwrap(): List.cons,

'first'.unwrap(): List.first,

'rest'.unwrap(): List.rest,

'+'.unwrap_op(): fun (x, y): x + y,

'-'.unwrap_op(): fun (x, y): x - y,

'*'.unwrap_op(): fun (x, y): x * y}

You can’t perform that action at this time.

interp rhombus first file unwrap search

Related Articles