Working on simplification (but the tree makes a priority error)

This commit is contained in:
Arthur Guiot committed 2019-12-30 20:18:56 -05:00
1 parent 7e7cb4e1d3
commit cbf1cc4e78
5 files changed
+37 -6

No files matched your search

@@ -31,7 +31,7 @@ public class ConstantNode: NSObject, Node {
/// Create a ConstantNode
/// - Parameter bn: BigNumber/BigDouble
public init(_ bn: BigNumber) {
self.content = bn.decimalDescription
self.content = bn.description
}
/// Create a ConstantNode
/// - Parameter int: Integer
@@ -47,8 +47,8 @@ public class ExpressionNode: NSObject, Node {
/// Compiles ExpressionNode to simpler node (useless here, but required by protocol)
public func compile() -> Node {
self.children = self.children.map { $0.compile() }
return self
guard let first = self.children.first else { return self }
return first.compile()
}
/// Converts ExpressionNode to BigNumber
public func evaluate(_ params: [String: BigNumber]) -> BigNumber {
@@ -60,7 +60,7 @@ public class OperatorNode: NSObject, Node {
let c1 = self.children[0].compile()
let c2 = self.children[1].compile()
if c1.type == "ContantNode" && c2.type == "ContantNode" {
if c1.type == "ConstantNode" && c2.type == "ConstantNode" {
let ev1 = c1.evaluate([:])
let ev2 = c2.evaluate([:])
switch self.content {
@@ -80,7 +80,7 @@ public class OperatorNode: NSObject, Node {
}
return self
return OperatorNode(self.content, children: [c1, c2])
}
/// Converts OperatorNode to BigNumber
public func evaluate(_ params: [String: BigNumber]) -> BigNumber {
@@ -0,0 +1,14 @@
//
// File.swift
//
//
// Created by Arthur Guiot on 2019-12-30.
//
import Foundation
class Simplify {
// func simple(_ expr: ExpressionNode) -> ExpressionNode {
// return expr.compile()
// }
}
+18 -1
View File
@@ -54,12 +54,29 @@ class NodeTests: XCTestCase {
XCTAssertEqual(try Parser("((4*2) - 3) +sqrt(4)").parse().toString(), "((4.0 * 2.0) - 3.0) + sqrt(4.0)")
} catch {
print(error.localizedDescription)
XCTFail()
}
}
func testCompile() {
do {
let p = Parser("x+y*2 + (4+5)/3")
let expression = try p.parse()
let comp = expression.compile()
XCTAssertEqual(comp.toString(), "x + y * 2.0 + 3.0")
XCTAssertEqual(try Parser("(4*2)").parse().compile().toString(), "8.0000")
} catch {
print(error.localizedDescription)
XCTFail()
}
}
static var allTests = [
("Constant Node", testConstantNode),
("Operator Node", testOperatorNode),
("Symbol Node", testSymboleNode)
("Symbol Node", testSymboleNode),
("Parser", testParser),
("Compiler", testCompile)
]
}