Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- @@scopes = Array.new()
- @@scopes.push(Hash.new)
- @@currentScope = 0
- @@output = "Output:\n"
- @@evaled = Array.new()
- @@statements = Array.new()
- @@functions = Hash.new()
- @@classes = Hash.new()
- @@lowestScope = 0
- @@capState = "public"
- def makeList(lines)
- list = Array.new()
- if lines.respond_to?('size')
- lines.each {|m|
- if m.respond_to?('size')
- m.each{|b|
- if b.respond_to?('size')
- makeList(b).each{|c|
- list << c
- }
- else
- list << b
- end
- }
- else
- list << m
- end
- }
- else
- list << lines
- end
- return list
- end
- class State
- def initialize(state)
- @state = state
- end
- def eval()
- ## @@capState = @state
- end
- end
- class Variable
- attr_accessor:name, :type, :value, :state
- def initialize(name, type, value)
- @value = value
- @name = name
- @type = type
- @state = @@capState
- end
- def setValue(value)
- if @type == ClassVariable
- @type = @value.class
- end
- if @type == String
- @type = FString
- elsif @type == Fixnum
- @type = Int
- end
- if value.is_a?(@type)
- @value = value
- elsif value.is_a?(ArithmeticStatement) && @type.to_s == "Int"
- while value.respond_to?('eval')
- value = value.eval()
- end
- @value = value
- else
- puts "ERROR: '"+@name[3..@name.size-1].to_s+"' is not a "+@type.to_s
- end
- end
- def eval()
- if @@scopes[@@currentScope][@name] == nil
- @@scopes[@@currentScope][@name] = self
- elsif @@scopes[@@currentScope][@name] != self
- puts "ERROR: variable '"+@name[3..@name.size-1].to_s+"' was already declared in this scope!"
- end
- #@@scopes[@@currentScope][@name] = self
- if !@value.is_a?(DaDaClassObject)
- while @value.respond_to?('eval')
- @value = @value.eval()
- end
- end
- return @value
- end
- end
- class ClassVariable
- attr_accessor:name, :type, :value, :state
- def initialize(name, type, value, state)
- @value = value
- @name = name
- @type = type
- @state = state
- end
- def setValue(value)
- if value.is_a?(@type)
- @value = value
- elsif value.is_a?(ArithmeticStatement) && @type.to_s == "Int"
- while value.respond_to?('eval')
- value = value.eval()
- end
- @value = value
- else
- puts "ERROR: '"+@name[3..@name.size-1].to_s+"' is not a "+@type.to_s
- end
- end
- def eval(parent)
- if parent.scope[@name] != nil && parent.scope[@name] != self
- puts "ERROR: variable '"+@name[3..@name.size-1].to_s+"' was already declared in this scope!"
- else
- if @state == "public"
- list = Array.new()
- list << PrintStatement.new(@value)
- parent.functions[@name] = DaDaClassFunction.new(parent.name, @name, Array.new(), list)
- end
- parent.scope[@name] = self
- end
- while @value.respond_to?('eval')
- @value = @value.eval()
- end
- return @value
- end
- end
- class ReturnStatement
- attr_accessor:value
- def initialize(value)
- @value = value
- end
- def eval()
- if @value.respond_to?('eval')
- @value = @value.eval()
- else
- @value
- end
- if @value.respond_to?('value')
- @value = @value.value
- end
- @value
- end
- end
- class BreakStatement
- #HEJ JAG ÄR TOM
- end
- class InputStatement
- def initialize()
- end
- def eval()
- puts gets.chomp
- end
- end
- class FunctionCall
- def initialize(name, args)
- @name = name
- @args = args
- end
- def eval()
- count = 0
- @args.each{|m|
- if m.respond_to?('eval')
- @args[count] = m.eval.value
- end
- if @args[count].is_a?(Fixnum)
- @args[count] = Int.new(@args[count])
- end
- if @args[count].is_a?(String)
- @args[count] = FString.new(@args[count])
- end
- count = count+1
- }
- @@functions[@name].call(@args)
- end
- end
- class ClassFunctionCall
- def initialize(var, function, args)
- @var = var
- @function = function
- @args = args
- end
- def eval()
- if findVar(@var).value.is_a?(FArray) || findVar(@var).value.is_a?(String) || findVar(@var).value.is_a?(FString) || findVar(@var).value.is_a?(Int)
- if @args.size < 1
- findVar(@var).value.send(@function)
- elsif @args.size == 1
- findVar(@var).value.send(@function, @args)
- elsif @args.size == 2
- findVar(@var).value.send(@function, @args[1], @args[0])
- end
- else
- vars = Hash.new
- func = findVar(@var).value.functions[@function]
- if func.state == "public" || func.state == "publicProtected" || func.state = "publicPrivate"
- funcs = findVar(@var).value.functions
- funcs.each{|m|
- if m[1].state = "private"
- m[1].state = "publicPrivate"
- elsif m[1].state = "protected"
- m[1].state = "publicProtected"
- end
- }
- cObj = findVar(@var).value
- vars = findVar(@var).value.vars
- @@currentScope = @@currentScope+1
- @@lowestScope = @@currentScope
- @@scopes.delete_at(@@currentScope)
- addScope()
- Variable.new("varself", cObj.class, cObj).eval()
- vars.each{|m|
- Variable.new(m[0], m[1].class, m[1].value).eval()
- }
- if vars.has_key?(@function)
- return vars[@function].value
- ## @@output = @@output + vars[@function].value.to_s+"\n"
- else
- ret = func.call(@args)
- @@scopes.delete_at(@@currentScope)
- @@currentScope = @@currentScope-1
- @@lowestScope = 0
- funcs.each{|m|
- if m[1].state == "publicPrivate"
- m[1].state = "private"
- elsif m[1].state == "publicProtected"
- m[1].state = "protected"
- end
- }
- return ret
- end
- @@scopes.delete_at(@@currentScope)
- @@currentScope = @@currentScope-1
- @@lowestScope = 0
- funcs.each{|m|
- if m[1].state == "publicPrivate"
- m[1].state = "private"
- elsif m[1].state == "publicProtected"
- m[1].state = "protected"
- end
- }
- end
- end
- end
- end
- class DaDaFunction
- attr_accessor:name, :state
- def initialize(className, funcName, args, lines)
- if className == "Int"
- className = "Fixnum"
- elsif className == "String"
- className = "FString"
- end
- @className = className
- @name = funcName
- @args = args
- @lines = lines
- @state = @@capState
- end
- def call(args)
- @@currentScope = @@currentScope +1
- addScope()
- if args.size() == @args.size()
- i = 0
- args.reverse.each{|m|
- @args[i].setValue(m)
- i = i +1
- }
- else
- puts "ERROR: "+@name.to_s+"(): wrong amount of arguments!"
- end
- @args.each { |m|
- if m.is_a?(Variable)
- m.eval()
- end
- }
- ret = ""
- @lines.each{|m|
- if m.respond_to?('size')
- m.each{|b|
- if b.is_a?(ReturnStatement)
- if @className == b.eval.class
- @@currentScope = @@currentScope -1
- return b.value
- end
- else
- ret = b.eval()
- end
- }
- elsif m.is_a?(ReturnStatement) || ret.is_a?(ReturnStatement)
- if @className == "Bool" && (m.eval == "false" || m.eval == "true")
- @@currentScope = @@currentScope -1
- return m.value
- elsif @className == "Fixnum" && m.eval.class.to_s == "Int"
- @@currentScope = @@currentScope -1
- return m.value
- elsif @className == m.eval.class.to_s
- @@currentScope = @@currentScope -1
- return m.value
- else
- puts "ERROR: "+m.value.to_s+" is not a "+@className
- return "ERROR: "+m.value.to_s+" is not a "+@className
- end
- else
- ret = m.eval()
- end
- if ret.is_a?(ReturnStatement)
- if @className == ret.eval.class.to_s
- @@currentScope = @@currentScope -1
- return ret.value
- else
- puts "ERROR: "+ret.value.to_s+" is not a "+@className
- return "ERROR: "+ret.value.to_s+" is not a "+@className
- end
- end
- }
- @@currentScope = @@currentScope -1
- end
- end
- class DaDaClassFunction
- attr_accessor:name, :state
- def initialize(className, funcName, args, lines)
- @className = className
- @name = funcName
- @args = args
- @lines = lines
- @state = @@capState
- end
- def call(args)
- @@currentScope = @@currentScope +1
- addScope()
- if args.size() == @args.size()
- i = 0
- args.reverse.each{|m|
- @args[i].setValue(m)
- i = i +1
- }
- else
- puts "ERROR: "+@name.to_s+"(): wrong amount of arguments!"
- end
- @args.each { |m|
- if m.is_a?(Variable)
- m.eval()
- end
- }
- @lines.each{|m|
- if m.respond_to?('size')
- m.each{|b|
- if b.is_a?(ReturnStatement)
- if @className == b.value.class
- @@currentScope = @@currentScope -1
- return b.value
- end
- else
- b.eval()
- end
- }
- elsif m.is_a?(ReturnStatement)
- if @className == m.value.class.to_s
- @@currentScope = @@currentScope -1
- return m.value
- else
- puts "ERROR: "+m.value.to_s+" is not a "+@className
- return "ERROR: "+m.value.to_s+" is not a "+@className
- end
- else
- m.eval()
- end
- }
- @@currentScope = @@currentScope -1
- end
- end
- class DaDaClassObject
- attr_accessor:vars, :functions, :name
- def initialize(className)
- @name = className
- @vars = Hash.new()
- @functions = Hash.new()
- end
- def eval()
- @vars = @@classes[@name].scope
- @functions = @@classes[@name].functions
- end
- end
- class DaDaClass
- attr_accessor:scope, :functions, :name
- def initialize(name, lines)
- @lines = lines
- @name = name
- @scope = Hash.new()
- @functions = Hash.new()
- end
- def eval()
- @lines.each{|m|
- if m.respond_to?('size')
- m.each{|b|
- if b.is_a?(ClassVariable)
- b.eval(self)
- else
- b.eval()
- end
- }
- else
- if m.is_a?(ClassVariable)
- m.eval(self)
- elsif m.is_a?(DaDaFunction)
- @functions[m.name] = m
- else
- m.eval()
- end
- end
- }
- end
- end
- class DaDaSubClass
- attr_accessor:scope, :functions, :name
- def initialize(name, superclass, lines)
- @lines = lines
- @superclass = superclass
- @name = name
- @scope = Hash.new()
- @functions = Hash.new()
- end
- def eval()
- @lines.each{|m|
- if m.respond_to?('size')
- m.each{|b|
- if b.is_a?(ClassVariable)
- b.eval(self)
- else
- b.eval()
- end
- }
- else
- if m.is_a?(ClassVariable)
- m.eval(self)
- elsif m.is_a?(DaDaFunction)
- @functions[m.name] = m
- else
- m.eval()
- end
- end
- }
- @@classes[@superclass].scope.each{|m|
- if !@scope.has_key?(m[0]) && (m[1].state.to_s == "public" || m[1].state.to_s == "protected")
- @scope[m[0]] = m[1]
- end
- }
- @@classes[@superclass].functions.each{|m|
- if !@functions.has_key?(m[0])
- @functions[m[0]] = m[1]
- end
- }
- end
- end
- def getCurrentScope()
- return @currentScope
- end
- class Scope
- attr_accessor:child_scopes, :variables
- @variables = Hash.new();
- def initialize()
- @name = "Global scope"
- end
- end
- class PrintStatement
- attr_accessor:value
- def initialize(val)
- @value = val
- end
- def eval()
- back = @value.clone
- while back.respond_to?('eval')
- back = back.eval()
- end
- while back.respond_to?('call')
- back = back.call()
- end
- if back.is_a?(Hash)
- back.each{|m|
- back = m[1].call(Array.new())
- }
- elsif back.is_a?(Array)
- ## @@output = @@output + back[back.size-1].eval().to_s+"\n"
- puts back[back.size-1].eval().to_s
- elsif back.is_a?(FArray)
- back.value.each{|m|
- ## @@output = @@output + m.eval().to_s+"\n"
- puts m.eval.to_s
- }
- else
- ## @@output = @@output.to_s + back.to_s+"\n"
- puts back.to_s
- end
- return back
- end
- end
- class StringArithmetics
- attr_accessor:operator, :left, :right
- def initialize(left, operator, right)
- @operator = operator
- @left = left
- @right = right
- end
- def eval()
- if @left.kind_of?(FString) && @right.kind_of?(ArithmeticStatement)
- return FString.new(instance_eval("@left.eval() #{@operator} @right.eval().eval().to_s"))
- elsif @left.kind_of?(FString) && @right.kind_of?(Int)
- return FString.new(instance_eval("@left.eval() #{@operator} @right.eval().to_s"))
- elsif @left.kind_of?(FString) && @right.kind_of?(FString)
- return FString.new(instance_eval("@left.eval() #{@operator} @right.eval()"))
- end
- end
- end
- class ArithmeticStatement
- attr_accessor:operator, :left, :right
- def initialize(left, operator, right)
- @operator = operator
- @left = left
- @right = right
- end
- def eval()
- if @left.is_a?(VarRep)
- @left = @left.eval()
- end
- if @right.is_a?(VarRep)
- @right = @right.eval()
- end
- if @left.kind_of?(ArithmeticStatement) && @right.kind_of?(ArithmeticStatement)
- return Int.new(instance_eval("#{@left.eval().eval()} #{@operator} #{@right.eval().eval()}"))
- elsif !@left.kind_of?(ArithmeticStatement) && !@right.kind_of?(ArithmeticStatement)
- return Int.new(instance_eval("#{@left.eval()} #{@operator} #{@right.eval()}"))
- elsif !@left.kind_of?(ArithmeticStatement) && @right.kind_of?(ArithmeticStatement)
- return Int.new(instance_eval("#{@left.eval()} #{@operator} #{@right.eval().eval()}"))
- elsif @left.kind_of?(ArithmeticStatement) && !@right.kind_of?(ArithmeticStatement)
- return Int.new(instance_eval("#{@left.eval().eval()} #{@operator} #{@right.eval()}"))
- end
- end
- end
- class Int
- @value = 0
- @error = false
- def initialize(int)
- if int.is_a?(Fixnum)
- @value = int
- elsif int.is_a?(Int)
- @value = int.value
- else
- @error = true
- puts "ERROR: "+int.to_s+" is not an Int!"
- end
- end
- def setValue(newValue)
- if newValue.is_a?(Fixnum)
- @value = newValue
- return self
- elsif newValue.is_a?(Int)
- @value = newValue.value
- return self
- else
- @error = true
- puts "ERROR: "+newValue.to_s+" is not an Int!"
- end
- end
- def eval()
- @value
- end
- def sort(array)
- temp = Array.new()
- array.each{|m|
- temp << m.value
- }
- temp.sort!
- retArray = Array.new()
- temp.each{|b|
- retArray << Int.new(b)
- }
- retArray
- end
- def rsort(array)
- temp = Array.new()
- array.each{|m|
- temp << m.value
- }
- temp.sort!
- retArray = Array.new()
- temp.reverse.each{|b|
- retArray << Int.new(b)
- }
- retArray
- end
- attr_accessor:value, :error
- end
- def checkScope(scope, var)
- end
- def printScopes()
- @@currentScope.downto(0).each{|m|
- print "Scope "+m.to_s+": "
- puts @@scopes[m].to_s
- }
- end
- def findVar(var)
- scope = @@currentScope
- @@currentScope.downto(@@lowestScope).each{|m|
- if @@scopes[m] != nil
- if @@scopes[m][var] != nil
- return @@scopes[m][var]
- end
- end
- }
- return "Error: Variable '"+var[3..var.size-1].to_s+"' was not declared!"
- end
- def setVar(var, newVal)
- scope = @@currentScope
- @@currentScope.downto(0).each{|m|
- if @@scopes[m] != nil
- if @@scopes[m][var] != nil
- return @@scopes[m][var].setValue(newVal)
- end
- end
- }
- puts "Error: Variable '"+var[3..var.size-1].to_s+"' was not declared!"
- end
- def setClassVar(var, newVal)
- scope = @@currentScope
- @@currentScope.downto(0).each{|m|
- if @@scopes[m] != nil
- if @@scopes[m][var] != nil
- return @@scopes[m][var].setValue(newVal)
- end
- end
- }
- puts "Error: Variable '"+var[3..var.size-1].to_s+"' was not declared!"
- end
- class VarRep
- attr_accessor:name, :scope
- def initialize(var)
- @scope = @@currentScope
- @name = var
- end
- def eval()
- findVar(@name)
- end
- end
- class VarChanger
- def initialize(var, newVal)
- @name = var
- @newVal = newVal
- end
- def eval()
- setClassVar(@name, @newVal)
- end
- end
- class ClassVarChanger
- def initialize(classVar, var, newVal)
- @classVar = classVar
- @name = var
- @newVal = newVal
- end
- def eval()
- findVar(@classVar).value.vars[@name].setValue(@newVal)
- if findVar(@classVar).value.functions[@name] != nil
- list = Array.new()
- list << PrintStatement.new(findVar(@classVar).value.vars[@name].value) # FIXA(ändra till return)
- findVar(@classVar).value.functions[@name] = DaDaClassFunction.new(@classVar, @name, Array.new(), list)
- end
- end
- end
- class ForeachStatement
- def initialize(array, variable, lines)
- @array = array
- @variable = variable
- @lines = lines
- end
- def eval()
- stop = false
- @array = @array.eval().value.value
- @@currentScope = @@currentScope +1
- addScope()
- @array.each{|m|
- @@scopes[@@currentScope][@variable] = m
- #puts @@scopes[@@currentScope][@variable]
- @lines.each{|b|
- if b.respond_to?('size')
- b.each{|c|
- if c.is_a?(BreakStatement)
- @@scopes.delete_at(@@currentScope)
- @@currentScope = @@currentScope -1
- return BreakStatement
- else
- ret = c.eval()
- end
- }
- elsif b.is_a?(BreakStatement)
- @@scopes.delete_at(@@currentScope)
- @@currentScope = @@currentScope -1
- return BreakStatement
- else
- ret = b.eval()
- end
- if ret == BreakStatement
- stop = true
- break
- end
- }
- if stop
- break
- end
- }
- @@scopes.delete_at(@@currentScope)
- @@currentScope = @@currentScope -1
- end
- end
- class WhileStatement
- def initialize(a, operator, b, lines)
- @a = a
- @operator = operator
- @b = b
- @lines = lines
- end
- def eval()
- ret = ""
- @@currentScope = @@currentScope +1
- addScope()
- while compare(@a, @operator, @b) == true
- @lines.each{|m|
- if m.respond_to?('size')
- m.each{|b|
- if b.is_a?(BreakStatement)
- @@scopes.delete_at(@@currentScope)
- @@currentScope = @@currentScope -1
- return BreakStatement
- else
- ret = b.eval()
- end
- }
- elsif m.is_a?(BreakStatement)
- @@scopes.delete_at(@@currentScope)
- @@currentScope = @@currentScope -1
- return BreakStatement
- else
- ret = m.eval()
- end
- if ret == BreakStatement
- @@scopes.delete_at(@@currentScope)
- @@currentScope = @@currentScope -1
- return BreakStatement
- end
- }
- end
- @@scopes.delete_at(@@currentScope)
- @@currentScope = @@currentScope -1
- end
- end
- def addScope()
- if @@scopes[@@currentScope] == nil
- @@scopes << Hash.new()
- end
- end
- class IfStatement
- def initialize(a, operator, b, lines)
- @a = a
- @operator = operator
- @b = b
- @lines = lines
- end
- def addelseifs(elseif)
- @stmts = Array.new()
- elseif.reverse.each{|m|
- @stmts << m
- }
- end
- def eval()
- ret = ""
- @@currentScope = @@currentScope +1
- addScope()
- if compare(@a, @operator, @b) == true
- @lines.each{|m|
- if m.respond_to?('size')
- m.each{|b|
- if b.is_a?(BreakStatement)
- @@scopes.delete_at(@@currentScope)
- @@currentScope = @@currentScope -1
- return BreakStatement
- elsif b.is_a?(ReturnStatement)
- @@scopes.delete_at(@@currentScope)
- @@currentScope = @@currentScope -1
- return b
- else
- ret = b.eval()
- end
- }
- elsif m.is_a?(BreakStatement)
- @@scopes.delete_at(@@currentScope)
- @@currentScope = @@currentScope -1
- return BreakStatement
- elsif m.is_a?(ReturnStatement)
- @@scopes.delete_at(@@currentScope)
- @@currentScope = @@currentScope -1
- return m
- else
- ret = m.eval()
- end
- if ret == BreakStatement
- @@scopes.delete_at(@@currentScope)
- @@currentScope = @@currentScope -1
- return BreakStatement
- elsif ret.is_a?(ReturnStatement)
- @@scopes.delete_at(@@currentScope)
- @@currentScope = @@currentScope -1
- return ret
- end
- }
- elsif @stmts != nil
- @stmts.each{|m|
- if m.respond_to?('eval')
- ret = m.eval()
- end
- if ret == BreakStatement
- @@scopes.delete_at(@@currentScope)
- @@currentScope = @@currentScope -1
- return BreakStatement
- end
- if ret.is_a?(ReturnStatement)
- @@scopes.delete_at(@@currentScope)
- @@currentScope = @@currentScope -1
- return ret
- end
- }
- end
- @@scopes.delete_at(@@currentScope)
- @@currentScope = @@currentScope -1
- end
- end
- class ElseIfStatement
- attr_accessor:evaled
- def initialize(a, operator, b, lines)
- @a = a
- @operator = operator
- @b = b
- @lines = lines
- end
- def eval()
- ret = ""
- if compare(@a, @operator, @b) == true
- @lines.each{|m|
- if m.respond_to?('size')
- m.each{|b|
- if b.is_a?(BreakStatement)
- return BreakStatement
- elsif b.is_a?(ReturnStatement)
- return b
- else
- ret = b.eval()
- end
- }
- elsif m.is_a?(BreakStatement)
- return BreakStatement
- elsif m.is_a?(ReturnStatement)
- return m
- else
- ret = m.eval()
- end
- if ret == BreakStatement
- return BreakStatement
- elsif ret.is_a?(ReturnStatement)
- return ret
- end
- }
- return true
- end
- return false
- end
- end
- class ElseStatement
- def initialize(lines)
- @lines = lines
- end
- def eval()
- @lines.each{|m|
- if m.respond_to?('size')
- m.each{|b|
- if b.is_a?(BreakStatement)
- return BreakStatement
- elsif b.is_a?(ReturnStatement)
- return b
- else
- ret = b.eval()
- end
- }
- elsif m.is_a?(BreakStatement)
- return BreakStatement
- elsif m.is_a?(ReturnStatement)
- return m
- else
- ret = m.eval()
- end
- if ret == BreakStatement
- return BreakStatement
- elsif ret.is_a?(ReturnStatement)
- return ret
- end
- }
- end
- end
- class FString
- @value = ""
- @error = false
- def initialize(string)
- if string.is_a?(String)
- @value = string
- elsif string.is_a?(FString)
- @value = string.value
- elsif string.is_a?(StringArithmetics)
- @value = string.eval()
- else
- @error = true
- puts "ERROR: "+string.to_s+" is not a String!"
- end
- end
- def setValue(newValue)
- if newValue.is_a?(String)
- @value = newValue
- return @value
- elsif newValue.is_a?(FString)
- @value = newValue.value
- return @value
- else
- @error = true
- puts "ERROR: "+newValue.to_s+" is not a String!"
- end
- end
- def eval()
- @value
- end
- def sort(array)
- temp = Array.new()
- array.each{|m|
- temp << m.value
- }
- temp.sort!
- retArray = Array.new()
- temp.each{|b|
- retArray << FString.new(b)
- }
- retArray
- end
- def rsort(array)
- temp = Array.new()
- array.each{|m|
- temp << m.value
- }
- temp.sort!
- retArray = Array.new()
- temp.reverse.each{|b|
- retArray << FString.new(b)
- }
- retArray
- end
- attr_accessor:value, :error
- end
- class Bool
- @value = "false"
- @error = false
- def initialize(bool)
- if bool == "true" || bool == "false"
- @value = bool
- elsif bool.is_a?(Bool)
- @value = bool.value
- else
- @error = true
- puts "ERROR: "+bool.to_s+" is not a Bool!"
- end
- end
- def setValue(newValue)
- if newValue == "true"
- @value = newValue
- return @value
- elsif newValue == "false"
- @value = newValue
- return @value
- elsif newValue.is_a?(Bool)
- @value = newValue.value
- return @value
- else
- @error = true
- puts "ERROR: "+newValue.to_s+" is not a Bool!"
- end
- end
- def eval()
- @value
- end
- attr_accessor:value, :error
- end
- class FArray
- attr_accessor:error
- @error = false
- def initialize(string)
- @type = string
- @type = instance_eval("@type")
- @value = Array.new()
- end
- def setValue(newValue)
- if newValue.is_a?(String)
- @value = newValue
- return @value
- elsif newValue.is_a?(FString)
- @value = newValue.value
- return @value
- else
- return newValue.to_s+" is not a String!"
- end
- end
- def append(val)
- if @type == val[0].class
- @value.push(val[0])
- else
- puts "ERROR: "+val[0].to_s+" is not a "+@type.to_s
- end
- end
- def delete(index)
- @value.delete_at(index[0].value)
- end
- def pop()
- @value.pop()
- end
- def insert(index, value)
- @value.insert(index.value, value)
- end
- def find(val)
- index = 0
- @value.each{|m|
- if m.value == val[0].value
- return index
- end
- index = index+1
- }
- return "ERROR: array does not contain "+val[0].value.to_s
- end
- def getIndex(index)
- if @value[index[0].value] != nil
- return @value[index[0].value]
- else
- return "ERROR: index "+index[0].value.to_s+" does not exist!"
- end
- end
- def sort()
- if @value.size > 0 && @value[0].respond_to?('sort')
- @value = @value[0].sort(@value)
- end
- end
- def rsort()
- if @value.size > 0 && @value[0].respond_to?('rsort')
- @value = @value[0].rsort(@value)
- end
- end
- attr_accessor:value
- end
- def compare(a, comp, b)
- if a.is_a?(VarRep) || b.is_a?(VarRep)
- a = a.eval()
- b = b.eval()
- if a.respond_to?('eval')
- a = a.eval()
- end
- if b.respond_to?('eval')
- b = b.eval()
- end
- else
- a = a.value()
- b = b.value()
- end
- if comp == "<="
- if a <= b
- return true
- end
- elsif comp == "<"
- if a < b
- return true
- end
- elsif comp == ">"
- if a > b
- return true
- end
- elsif comp == "=="
- if a == b
- return true
- end
- elsif comp == ">="
- if a >= b
- return true
- end
- elsif comp == "!="
- if a != b
- return true
- end
- elsif comp == "in"
- #gör något
- elsif comp == "!in"
- #något
- end
- return false
- end
Add Comment
Please, Sign In to add comment