Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
macfeteria committed Feb 19, 2019
1 parent 9fc8d57 commit 5edd8a9
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 304 deletions.
50 changes: 3 additions & 47 deletions Sources/swiftmonkey/Ast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ public protocol Node {
}

public protocol Statement: Node {
func statementNode() -> Void
}

public protocol Expression: Node {
func expressionNode() -> Void
}

extension Expression {
Expand Down Expand Up @@ -54,9 +52,6 @@ struct LetStatement: Statement {
var name: Identifier
var value: Expression?

func statementNode() {
}

func tokenLiteral() -> String {
return token.literal
}
Expand All @@ -79,9 +74,6 @@ struct ReturnStatement: Statement {
func tokenLiteral() -> String {
return token.literal
}

func statementNode() {
}

func string() -> String {
var result = "\(token.literal) "
Expand All @@ -102,9 +94,6 @@ struct ExpressionStatement: Statement {
return token.literal
}

func statementNode() {
}

func string() -> String {
if let value = expression {
return value.string()
Expand All @@ -118,9 +107,6 @@ struct Identifier: Expression {
var token:Token
var value:String

func expressionNode() {
}

func tokenLiteral() -> String {
return token.literal
}
Expand All @@ -133,8 +119,6 @@ struct Identifier: Expression {
struct IntegerLiteral: Expression {
var token:Token
var value:Int
func expressionNode() {
}

func tokenLiteral() -> String {
return token.literal
Expand All @@ -148,8 +132,6 @@ struct IntegerLiteral: Expression {
struct Boolean: Expression {
var token:Token
var value:Bool
func expressionNode() {
}

func tokenLiteral() -> String {
return token.literal
Expand All @@ -165,9 +147,6 @@ struct PrefixExpression: Expression {
var operatorLiteral:String
var right: Expression?

func expressionNode() {
}

func tokenLiteral() -> String {
return token.literal
}
Expand All @@ -180,9 +159,8 @@ struct PrefixExpression: Expression {
}

struct InvalidExpression: Expression {
let token:Token = Token(tokenType: TokenType.ILLEGAL, literal: "")
func expressionNode() {
}
let token:Token = Token(type: TokenType.ILLEGAL, literal: "")

func tokenLiteral() -> String {
return token.literal
}
Expand All @@ -197,9 +175,6 @@ struct InfixExpression: Expression {
var operatorLiteral:String
var right: Expression?

func expressionNode() {
}

func tokenLiteral() -> String {
return token.literal
}
Expand All @@ -215,9 +190,7 @@ struct InfixExpression: Expression {
struct BlockStatement: Expression {
var token:Token
var statements = [Statement]()
func expressionNode() {
}


func tokenLiteral() -> String {
return token.literal
}
Expand All @@ -236,9 +209,6 @@ struct IfExpression: Expression {
var consequence: BlockStatement
var alternative: BlockStatement?

func expressionNode() {
}

func tokenLiteral() -> String {
return token.literal
}
Expand All @@ -257,9 +227,6 @@ struct FunctionLiteral: Expression {
var parameters:[Identifier]
var body: BlockStatement

func expressionNode() {
}

func tokenLiteral() -> String {
return token.literal
}
Expand All @@ -281,9 +248,6 @@ struct CallExpression: Expression {
var function:Expression
var arguments: [Expression]

func expressionNode() {
}

func tokenLiteral() -> String {
return token.literal
}
Expand All @@ -303,8 +267,6 @@ struct CallExpression: Expression {
struct StringLiteral: Expression {
var token:Token
var value:String
func expressionNode() {
}

func tokenLiteral() -> String {
return token.literal
Expand All @@ -319,8 +281,6 @@ struct StringLiteral: Expression {
struct ArrayLiteral: Expression {
var token:Token
var elements: [Expression]
func expressionNode() {
}

func tokenLiteral() -> String {
return token.literal
Expand All @@ -342,8 +302,6 @@ struct IndexExpression: Expression {
var token:Token
var left:Expression
var index:Expression
func expressionNode() {
}

func tokenLiteral() -> String {
return token.literal
Expand All @@ -366,8 +324,6 @@ struct HashLiteral: Expression {

var token:Token
var pairs:[ExpressionKey:Expression]
func expressionNode() {
}

func tokenLiteral() -> String {
return token.literal
Expand Down
1 change: 0 additions & 1 deletion Sources/swiftmonkey/Evaluator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import Foundation


public struct Evaluator {
static let TRUE = BooleanObj(value: true)
static let FALSE = BooleanObj(value: false)
Expand Down
48 changes: 24 additions & 24 deletions Sources/swiftmonkey/Lexer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,49 +38,49 @@ public class Lexer {
if peekChar() == "=" {
let curent = ch
readChar()
tok = Token(tokenType: TokenType.EQUAL, literal: String("\(curent)\(ch)"))
tok = Token(type: TokenType.EQUAL, literal: String("\(curent)\(ch)"))
} else {
tok = Token(tokenType: TokenType.ASSIGN, literal: String(ch))
tok = Token(type: TokenType.ASSIGN, literal: String(ch))
}
case ";": tok = Token(tokenType: TokenType.SEMICOLON, literal: String(ch))
case "(": tok = Token(tokenType: TokenType.LPAREN, literal: String(ch))
case ")": tok = Token(tokenType: TokenType.RPAREN, literal: String(ch))
case ",": tok = Token(tokenType: TokenType.COMMA, literal: String(ch))
case ";": tok = Token(type: TokenType.SEMICOLON, literal: String(ch))
case "(": tok = Token(type: TokenType.LPAREN, literal: String(ch))
case ")": tok = Token(type: TokenType.RPAREN, literal: String(ch))
case ",": tok = Token(type: TokenType.COMMA, literal: String(ch))

case "+": tok = Token(tokenType: TokenType.PLUS, literal: String(ch))
case "-": tok = Token(tokenType: TokenType.MINUS, literal: String(ch))
case "/": tok = Token(tokenType: TokenType.SLASH, literal: String(ch))
case "*": tok = Token(tokenType: TokenType.ASTERISK, literal: String(ch))
case "+": tok = Token(type: TokenType.PLUS, literal: String(ch))
case "-": tok = Token(type: TokenType.MINUS, literal: String(ch))
case "/": tok = Token(type: TokenType.SLASH, literal: String(ch))
case "*": tok = Token(type: TokenType.ASTERISK, literal: String(ch))
case "!":
if peekChar() == "=" {
let curent = ch
readChar()
tok = Token(tokenType: TokenType.NOTEQUAL, literal: String("\(curent)\(ch)"))
tok = Token(type: TokenType.NOTEQUAL, literal: String("\(curent)\(ch)"))
} else {
tok = Token(tokenType: TokenType.BANG, literal: String(ch))
tok = Token(type: TokenType.BANG, literal: String(ch))
}
case "<": tok = Token(tokenType: TokenType.LESSTHAN, literal: String(ch))
case ">": tok = Token(tokenType: TokenType.GREATER, literal: String(ch))
case "<": tok = Token(type: TokenType.LESSTHAN, literal: String(ch))
case ">": tok = Token(type: TokenType.GREATER, literal: String(ch))

case "{": tok = Token(tokenType: TokenType.LBRACE, literal: String(ch))
case "}": tok = Token(tokenType: TokenType.RBRACE, literal: String(ch))
case "\0": tok = Token(tokenType: TokenType.EOF, literal: String(ch))
case "{": tok = Token(type: TokenType.LBRACE, literal: String(ch))
case "}": tok = Token(type: TokenType.RBRACE, literal: String(ch))
case "\0": tok = Token(type: TokenType.EOF, literal: String(ch))
case "\"":
let lit = readString()
return Token(tokenType: TokenType.STRING, literal: lit)
case "[": tok = Token(tokenType: TokenType.LBRACKET, literal: String(ch))
case "]": tok = Token(tokenType: TokenType.RBRACKET, literal: String(ch))
case ":": tok = Token(tokenType: TokenType.COLON, literal: String(ch))
return Token(type: TokenType.STRING, literal: lit)
case "[": tok = Token(type: TokenType.LBRACKET, literal: String(ch))
case "]": tok = Token(type: TokenType.RBRACKET, literal: String(ch))
case ":": tok = Token(type: TokenType.COLON, literal: String(ch))
default:
if isLetter(char:ch) {
let lit = readIdentifier()
let type = lookupIdent(ident: lit)
return Token(tokenType: type, literal: lit)
return Token(type: type, literal: lit)
} else if isDigit(char:ch) {
let lit = readNumber()
return Token(tokenType: TokenType.INT, literal: lit)
return Token(type: TokenType.INT, literal: lit)
} else {
return Token(tokenType: TokenType.ILLEGAL, literal: String(ch))
return Token(type: TokenType.ILLEGAL, literal: String(ch))
}
}
readChar()
Expand Down
Loading

0 comments on commit 5edd8a9

Please sign in to comment.