Writing your own arithmetic language in F#.

During a programming languages course, we were asked to write an arithmetic language. The exercise was meant to teach some fundamental ideas regarding what a programming language is.

So without further delay, here is the result of my first arithmetic language.

type AE =
    | Num of int
    | Sum of AE * AE
 
let rec Evaluate expr =
    match expr with
        | Num (x) -> x
        | Sum (l, r) ->
            let (lv, rv) = (Evaluate l, Evaluate r)
            lv + rv
 
let rec numNums expr =
    match expr with
        | Num (x) -> 1
        | Sum (l, r) ->
            let (lc, rc) = (numNums l, numNums r)
            lc + rc
 
let rec numSums expr =
    match expr with
        | Num (x) -> 0
        | Sum (l, r) ->
            let (lc, rc) = (numSums l, numSums r)
            1 + lc + rc

There are still many features that haven’t yet been implemented, but this illustrates the basic concepts that reign over the basic design of a programming language.

This entry was posted in Programming and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.