forked from ming1016/study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeGenerator.swift
More file actions
57 lines (53 loc) · 1.5 KB
/
CodeGenerator.swift
File metadata and controls
57 lines (53 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// CodeGeneratorFromJSToOC.swift
// HTN
//
// Created by DaiMing on 2018/5/2.
//
import Foundation
public class CodeGenerator {
public var code = ""
public init(_ input:String) {
print("Input code is:")
print(input)
let ast = JTransformer(input).ast
for aNode in ast {
code.append(recGeneratorCode(aNode))
}
print("The code generated:")
print(code)
}
public func recGeneratorCode(_ node:JNode) -> String {
var code = ""
if node.type == .ExpressionStatement {
for aExp in node.expressions {
code.append(recGeneratorCode(aExp))
}
}
if node.type == .CallExpression {
code.append(node.callee.name)
code.append("(")
if node.arguments.count > 0 {
for (index,arg) in node.arguments.enumerated() {
code.append(recGeneratorCode(arg))
if index != node.arguments.count - 1 {
code.append(", ")
}
}
}
code.append(")")
}
if node.type == .Identifier {
code.append(node.name)
}
if node.type == .NumberLiteral {
switch node.numberType {
case .float:
code.append(String(node.floatValue))
case .int:
code.append(String(node.intValue))
}
}
return code
}
}