Skip to content

Commit

Permalink
Fixup Remaining Eval Tests
Browse files Browse the repository at this point in the history
Introduce the form and symbol factories, and use them to implement the
remaining disabled `eval` tests.
  • Loading branch information
iwillspeak committed Jun 21, 2024
1 parent 0c179bc commit 4aede6a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 71 deletions.
46 changes: 40 additions & 6 deletions src/Feersum.CompilerServices/Syntax/Factories.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ open Firethorn.Green
open Firethorn.Red
open Serehfa

let private tok kind text =
GreenToken.Create(kind |> SyntaxUtils.astToGreen, text) |> GreenElement.Token

let private atmosphere = tok AstKind.ATMOSPHERE
let private space = atmosphere " "

let private constant kind value =
new Constant(
SyntaxNode.CreateRoot(
GreenNode.Create(
AstKind.CONSTANT |> SyntaxUtils.astToGreen,
[ GreenToken.Create(kind |> SyntaxUtils.astToGreen, Write.GetExternalRepresentation(value))
|> GreenElement.Token ]
[ tok kind (Write.GetExternalRepresentation(value)) ]
)
)
)


/// Create a Numeric Value Constant
///
/// Emits a syntax tree representing a single number
Expand Down Expand Up @@ -46,13 +50,43 @@ let quoted (e: Expression) =
SyntaxNode.CreateRoot(
GreenNode.Create(
AstKind.QUOTED_DATUM |> SyntaxUtils.astToGreen,
[ GreenToken.Create(AstKind.QUOTE |> SyntaxUtils.astToGreen, "'")
|> GreenElement.Token
e.RawNode.Green |> GreenElement.Node ]
[ tok AstKind.QUOTE "'"; e.RawNode.Green |> GreenElement.Node ]
)
)
)

/// Wrap a String as an Identifier Node
///
/// Produces a single well-formed symbol node contianing a single identifier.
let symbol ident =
new Symbol(
SyntaxNode.CreateRoot(
GreenNode.Create(
AstKind.SYMBOL |> SyntaxUtils.astToGreen,
[ tok AstKind.IDENTIFIER (Write.GetExternalRepresentation(new Ident(ident))) ]
)
)
)

/// Wrap a List of Expressions as a Form
///
/// Produces a simple well-formed form expression from a list of containing expressions.
let form (exprs: Expression list) =
let close = tok AstKind.CLOSE_PAREN ")"
let toNode (x: Expression) = x.RawNode.Green |> GreenElement.Node

let rec mapTail (exprs: Expression list) =
match exprs with
| [] -> [ close ]
| [ single ] -> [ single |> toNode; close ]
| head :: tail -> (head |> toNode) :: (space :: (mapTail tail))

new Form(
SyntaxNode.CreateRoot(
GreenNode.Create(AstKind.FORM |> SyntaxUtils.astToGreen, tok AstKind.OPEN_PAREN "(" :: mapTail exprs)
)
)

/// Wrap Expression as Script
///
/// Creates a new root `ScriptProgram` from the given `Expression`
Expand Down
2 changes: 1 addition & 1 deletion src/Serehfa/Ident.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public Ident(string id)
_id = string.Intern(id);
}

public bool IsSimple => _id.All(c => char.IsLetterOrDigit(c));
public bool IsSimple => _id.All(c => char.IsLetterOrDigit(c)) && _id.Length > 0;

public string Raw => _id;

Expand Down
92 changes: 28 additions & 64 deletions test/Feersum.Tests/EvalTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -50,71 +50,35 @@ let ``evaluate seqs`` () =
let ``Evaluate empty program`` () =
Assert.Equal("; Unspecified value", interpProg ([] |> program))

// FIXME: Eval of legacy node types
[<Fact>]
let ``Evaluate lambdas returns`` () =
Assert.Equal(
"123",
interpScr (
form [ form [ symbol "lambda"; form [ symbol "x" ]; symbol "x" ]; numVal 123.0 ]
|> scriptProgram
)
)

[<Fact>]
let ``Evaluate builtins`` () =
Assert.Equal("19", interpScr (form [ symbol "+"; numVal 10.0; numVal 9.0 ] |> scriptProgram))

Assert.Equal("901", interpScr (form [ symbol "+"; numVal 901.0 ] |> scriptProgram))

Assert.Equal("90", interpScr (form [ symbol "*"; numVal 10.0; numVal 9.0 ] |> scriptProgram))

Assert.Equal(
"901",
interpScr (
form [ symbol "+"; form [ symbol "*"; numVal 100.0; numVal 9.0 ]; numVal 1.0 ]
|> scriptProgram
)
)

Assert.Equal("1", interpScr (form [ symbol "-"; numVal 10.0; numVal 9.0 ] |> scriptProgram))

// [<Fact>]
// let ``Evaluate lambdas returns`` () =
// Assert.Equal(
// "123",
// feeri (
// Form
// [ Form
// [ Ident "lambda" |> node
// Form [ Ident "x" |> node ] |> node
// Ident "x" |> node ]
// |> node
// Number 123.0 |> constant ]
// |> node
// )
// )

// [<Fact>]
// let ``Evaluate builtins`` () =
// Assert.Equal(
// "19",
// feeri (
// Form [ Ident "+" |> node; Number 10.0 |> constant; Number 9.0 |> constant ]
// |> node
// )
// )

// Assert.Equal("901", feeri (Form [ Ident "+" |> node; Number 901.0 |> constant ] |> node))

// Assert.Equal(
// "90",
// feeri (
// Form [ Ident "*" |> node; Number 10.0 |> constant; Number 9.0 |> constant ]
// |> node
// )
// )

// Assert.Equal(
// "901",
// feeri (
// Form
// [ Ident "+" |> node
// Form [ Ident "*" |> node; Number 100.0 |> constant; Number 9.0 |> constant ]
// |> node
// Number 1.0 |> constant ]
// |> node
// )
// )

// Assert.Equal(
// "1",
// feeri (
// Form [ Ident "-" |> node; Number 10.0 |> constant; Number 9.0 |> constant ]
// |> node
// )
// )

// Assert.Equal(
// "2",
// feeri (
// Form [ Ident "/" |> node; Number 16.0 |> constant; Number 8.0 |> constant ]
// |> node
// )
// )
Assert.Equal("2", interpScr (form [ symbol "/"; numVal 16.0; numVal 8.0 ] |> scriptProgram))

[<Theory>]
[<InlineData("(+ 3 4)", "7")>]
Expand Down
30 changes: 30 additions & 0 deletions test/Feersum.Tests/SyntaxFactoriesTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,33 @@ let ``quotation exprs`` () =
| _ -> failwith "Node strucutre invalid"

checkReparse node

[<Fact>]
let ``simple forms`` () =
let node = form [ numVal 123; boolVal false ]

Assert.Equal("(123 #f)", node.Text)

Assert.True(node.DottedTail.IsNone)

match node with
| Form([ Constant(Some(NumVal 123.0)); Constant(Some(BoolVal false)) ]) -> ()
| _ -> failwith "Node structure invalid"

checkReparse node

[<Theory>]
[<InlineData("hello", "hello")>]
[<InlineData("scheme program", "|scheme program|")>]
[<InlineData("\t\n", "|\\x9;\\xA;|")>]
[<InlineData("", "||")>]
let ``identifier symbol exprs`` ident expected =
let node = symbol ident

Assert.Equal(expected, node.Text)

match node with
| Symbol id -> Assert.Equal(ident, id)
| _ -> failwith "Node strucutre invalid"

checkReparse node

0 comments on commit 4aede6a

Please sign in to comment.