Skip to content

Commit

Permalink
(WIP, almost) Handle match expressions in new interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
StachuDotNet committed Aug 14, 2024
1 parent 4b5b486 commit 2c4303b
Show file tree
Hide file tree
Showing 8 changed files with 709 additions and 602 deletions.
52 changes: 52 additions & 0 deletions backend/src/LibExecution/Interpreter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,57 @@ let rec execute
vmState.registers[copyTo] <- vmState.registers[copyFrom]
return! execute exeState vmState (counter + 1)

| MatchValue(valueReg, pat, failJump) ->
let matches, vars =
match pat, vmState.registers[valueReg] with
| MPUnit, DUnit -> true, []

| MPBool l, DBool r -> l = r, []

| MPInt8 l, DInt8 r -> l = r, []
| MPUInt8 l, DUInt8 r -> l = r, []
| MPInt16 l, DInt16 r -> l = r, []
| MPUInt16 l, DUInt16 r -> l = r, []
| MPInt32 l, DInt32 r -> l = r, []
| MPUInt32 l, DUInt32 r -> l = r, []
| MPInt64 l, DInt64 r -> l = r, []
| MPUInt64 l, DUInt64 r -> l = r, []
| MPInt128 l, DInt128 r -> l = r, []
| MPUInt128 l, DUInt128 r -> l = r, []

| MPFloat l, DFloat r -> l = r, []

| MPChar l, DChar r -> l = r, []
| MPString l, DString r -> l = r, []

// TODO
// | MPList of List<MatchPattern>

// | MPListCons of head : MatchPattern * tail : MatchPattern

// | MPTuple of
// first : MatchPattern *
// second : MatchPattern *
// theRest : List<MatchPattern>

| MPVariable name, dv -> true, [ (name, dv) ]

| _ -> false, []

if matches then
let vmState =
vars
|> List.fold
(fun vmState (varName, value) ->
{ vmState with
symbolTable = Map.add varName value vmState.symbolTable })
vmState
return! execute exeState vmState (counter + 1)
else
return! execute exeState vmState (counter + failJump + 1)



| ExtractTupleItems(extractFrom, firstReg, secondReg, restRegs) ->
match vmState.registers[extractFrom] with
| DTuple(first, second, rest) ->
Expand All @@ -133,6 +184,7 @@ let rec execute
| _ -> return DString "Error: Expected a tuple for decomposition"

| Fail _rte -> return DUnit // TODO
| MatchUnmatched -> return DUnit // TODO
}


Expand Down
66 changes: 33 additions & 33 deletions backend/src/LibExecution/ProgramTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -124,35 +124,35 @@ type LetPattern =
theRest : List<LetPattern>
| LPVariable of id * name : string

// /// Used for pattern matching in a match statement
// type MatchPattern =
// | MPUnit of id
/// Used for pattern matching in a match statement
type MatchPattern =
| MPUnit of id

// | MPBool of id * bool
| MPBool of id * bool

// | MPInt8 of id * int8
// | MPUInt8 of id * uint8
// | MPInt16 of id * int16
// | MPUInt16 of id * uint16
// | MPInt32 of id * int32
// | MPUInt32 of id * uint32
// | MPInt64 of id * int64
// | MPUInt64 of id * uint64
// | MPInt128 of id * System.Int128
// | MPUInt128 of id * System.UInt128
| MPInt8 of id * int8
| MPUInt8 of id * uint8
| MPInt16 of id * int16
| MPUInt16 of id * uint16
| MPInt32 of id * int32
| MPUInt32 of id * uint32
| MPInt64 of id * int64
| MPUInt64 of id * uint64
| MPInt128 of id * System.Int128
| MPUInt128 of id * System.UInt128

// | MPFloat of id * Sign * string * string
| MPFloat of id * Sign * string * string

// | MPChar of id * string
// | MPString of id * string
| MPChar of id * string
| MPString of id * string

// | MPList of id * List<MatchPattern>
// | MPListCons of id * head : MatchPattern * tail : MatchPattern
// | MPTuple of id * MatchPattern * MatchPattern * List<MatchPattern>
| MPList of id * List<MatchPattern>
| MPListCons of id * head : MatchPattern * tail : MatchPattern
| MPTuple of id * MatchPattern * MatchPattern * List<MatchPattern>

// | MPEnum of id * caseName : string * fieldPats : List<MatchPattern>
//| MPEnum of id * caseName : string * fieldPats : List<MatchPattern>

// | MPVariable of id * string
| MPVariable of id * string

type BinaryOperation =
| BinOpAnd
Expand Down Expand Up @@ -260,15 +260,15 @@ type Expr =
// /// `(1 + 2) |> fnName |> (+) 3`
// | EPipe of id * Expr * List<PipeExpr>

// /// Supports `match` expressions
// /// ```fsharp
// /// match x + 2 with // arg
// /// | pattern -> expr // cases[0]
// /// | pattern -> expr
// /// | ...
// /// ```
// // cases is a list to represent when a user starts typing but doesn't complete it
// | EMatch of id * arg : Expr * cases : List<MatchCase>
/// Supports `match` expressions
/// ```fsharp
/// match x + 2 with // arg
/// | pattern -> expr // cases[0]
/// | pattern -> expr
/// | ...
/// ```
// cases is a list to represent when a user starts typing but doesn't complete it
| EMatch of id * arg : Expr * cases : List<MatchCase>

// <summary>
// Composed of binding pattern, the expression to create bindings for,
Expand Down Expand Up @@ -353,7 +353,7 @@ type Expr =
// NameResolution<FQConstantName.FQConstantName>


//and MatchCase = { pat : MatchPattern; whenCondition : Option<Expr>; rhs : Expr }
and MatchCase = { pat : MatchPattern; whenCondition : Option<Expr>; rhs : Expr }

and StringSegment =
| StringText of string
Expand Down Expand Up @@ -409,7 +409,7 @@ module Expr =
// | ERecord(id, _, _)
// | ERecordUpdate(id, _, _)
// | EEnum(id, _, _, _)
// | EMatch(id, _, _)
| EMatch(id, _, _)
-> id

// module PipeExpr =
Expand Down
Loading

0 comments on commit 2c4303b

Please sign in to comment.