Skip to content

Commit b639ace

Browse files
committed
✨ Reverse polish notation printing added
1 parent 0a5ca54 commit b639ace

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

PascalInterpreter.playground/Contents.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ interpeter.eval()
1818

1919
let parser = Parser("2 * (7 + 3) ")
2020
print(parser.expr())
21+
22+
let rpn = RPN("(5 + 3) * 12 / 3")
23+
rpn.eval()

SwiftPascalInterpreter/SwiftPascalInterpreter.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
F3A550021FD9BBEF003476D9 /* AST.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3A550011FD9BBEF003476D9 /* AST.swift */; };
2020
F3A550041FD9BC2A003476D9 /* Parser.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3A550031FD9BC2A003476D9 /* Parser.swift */; };
2121
F3A550061FD9C209003476D9 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3A550051FD9C209003476D9 /* Utils.swift */; };
22+
F3D503C11FDA834600B27819 /* Notations.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3D503C01FDA834600B27819 /* Notations.swift */; };
2223
/* End PBXBuildFile section */
2324

2425
/* Begin PBXContainerItemProxy section */
@@ -47,6 +48,7 @@
4748
F3A550011FD9BBEF003476D9 /* AST.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AST.swift; sourceTree = "<group>"; };
4849
F3A550031FD9BC2A003476D9 /* Parser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Parser.swift; sourceTree = "<group>"; };
4950
F3A550051FD9C209003476D9 /* Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = "<group>"; };
51+
F3D503C01FDA834600B27819 /* Notations.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Notations.swift; sourceTree = "<group>"; };
5052
/* End PBXFileReference section */
5153

5254
/* Begin PBXFrameworksBuildPhase section */
@@ -74,6 +76,7 @@
7476
F36034081FD8589600CC81C3 /* SwiftPascalInterpreter.h */,
7577
F36034091FD8589600CC81C3 /* Info.plist */,
7678
F3474F5E1FD858FD003336FA /* Extensions.swift */,
79+
F3D503C01FDA834600B27819 /* Notations.swift */,
7780
F3A550051FD9C209003476D9 /* Utils.swift */,
7881
);
7982
name = "Supporting files";
@@ -259,6 +262,7 @@
259262
buildActionMask = 2147483647;
260263
files = (
261264
F3474F591FD858BA003336FA /* Interpreter.swift in Sources */,
265+
F3D503C11FDA834600B27819 /* Notations.swift in Sources */,
262266
F3474F5F1FD858FD003336FA /* Extensions.swift in Sources */,
263267
F3A550041FD9BC2A003476D9 /* Parser.swift in Sources */,
264268
F3A550061FD9C209003476D9 /* Utils.swift in Sources */,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Notations.swift
3+
// SwiftPascalInterpreter
4+
//
5+
// Created by Igor Kulman on 08/12/2017.
6+
// Copyright © 2017 Igor Kulman. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
/**
12+
Reverse polish notation
13+
*/
14+
public class RPN {
15+
private let parser: Parser
16+
17+
public init(_ text: String) {
18+
parser = Parser(text)
19+
}
20+
21+
public func eval() -> String {
22+
let node = parser.expr()
23+
return visit(node)
24+
}
25+
26+
private func visit(_ node: AST) -> String {
27+
switch node {
28+
case let .number(value):
29+
return "\(value)"
30+
case let .binaryOperation(left: left, operation: operation, right: right):
31+
return "\(visit(left)) \(visit(right)) \(operation.shortDescription)"
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)