dimanche 18 novembre 2018

OCaml pattern matching scoping issues when appending to Hash Tables

I'm working on an interpreter assignment in OCaml in which I have to bind values. For example I might want to bind the value "A" to the value 3.

I'm trying to use the hash table module in ocaml to accomplishments for easy storage and lookups but there seem to be multiple problems related to scoping, and return types.

What I want to do is when the case in which the second value popped off the stack is of type name I want to bind NAME to whatever the first value popped was and then append UNIT to the stack. Unfortunately for some reason my solution doesn't work :( Can anyone help me???

Message me so we can maybe google hangouts/CodePen and I can provide more information. Thanks!

open Hashtbl;;



let bound_values = Hashtbl.create 123456



type stackVal =

INT of int

| STR of string

| BOOL of bool

| NAME of string

| UNIT of unit

| ERROR



let callBind (stk : stackVal list) : stackVal list =

match (stk) with

x::NAME(y)::stk -> UNIT(Hashtbl.add bound_values y x)::stk

| _ -> ERROR::stk

Aucun commentaire:

Enregistrer un commentaire