Class: AmberVM::Interpreter::CoreCall

Inherits:
Element show all
Defined in:
lib/amber/interpreter.rb

Overview

A core call is a call to the direct core functions and libraries AmberVM proides. this is used to write core libraries for different languages.

Direct Known Subclasses

SimpleCoreCall

Instance Attribute Summary collapse

Attributes inherited from Element

#pos

Instance Method Summary collapse

Constructor Details

#initialize(function, arguments, pos = nil) ⇒ CoreCall

The constructor. function can either be a block object or a function class.

Arguments is a list of the arguments to the function.



1295
1296
1297
1298
1299
# File 'lib/amber/interpreter.rb', line 1295

def initialize function,  arguments, pos = nil
  super(pos)
  @function = function
  @arguments = arguments
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



1289
1290
1291
# File 'lib/amber/interpreter.rb', line 1289

def arguments
  @arguments
end

#functionObject (readonly)

Returns the value of attribute function.



1290
1291
1292
# File 'lib/amber/interpreter.rb', line 1290

def function
  @function
end

Instance Method Details

#data_typeObject



1341
1342
1343
# File 'lib/amber/interpreter.rb', line 1341

def data_type
  AmberVM::Functions[@function] ? AmberVM::Functions[@function].data_type : :any
end

#execute(env) ⇒ Object

When executed the CoreCall it will call one of the AmberVM’s library functions



1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
# File 'lib/amber/interpreter.rb', line 1346

def execute env
  AmberVM::debug "Executing CoreCall... args: #{@arguments.inspect}" if $DEBUG
  args = nil
  # The function is a anonymous function
 
  # Get the function from he globals
  if not fun = AmberVM::Functions[@function]
    raise RuntimeError.new("Function Not found!", @pos[0], @pos[1], @pos[2])
  end
  # Test if the arguments should be executed
  if fun.execargs
    # The arges get executed
    args = @arguments.map do |arg|
      a = arg.execute env
    end
  else
    args = @arguments
  end
  args.map! do |a|
    a = a.is_a?(AmberVM::Interpreter::VariableStorage) ? a.val : a
    a
  end
  # Call the function
  begin
    f = fun.call(args, env,  @pos)
    f
  rescue Exception => e
    raise e
    raise RuntimeError.new("Function failed to execute: #{e}", @pos[0], @pos[1], @pos[2])
  end
end

#optimize(variables = {}) ⇒ Object



1332
1333
1334
1335
1336
1337
1338
1339
# File 'lib/amber/interpreter.rb', line 1332

def optimize variables = {}
  arguments = @arguments.map!{ |a| a.optimize variables}
  if fun = AmberVM::Functions[@function]
    AmberVM::Interpreter::SimpleCoreCall.new(fun, arguments, @pos)
  else 
    CoreCall.new(@function, arguments, @pos)
  end
end

#pretty_print(q) ⇒ Object



1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
# File 'lib/amber/interpreter.rb', line 1301

def pretty_print(q)
  binary = {:sub => '-', :add => '+', :mul => '*', :div => '/', :mod => '%', :shl => '<<', :shr => '>>',
            :cmp => '<=>', :eq => '==', :gt => '>', :gte => '>=', :lt => '<', :lte => '<=',
            :bitwise_and => '&', :bitwise_or => '|', :bitwise_xor => '^',
            :and => '&&', :or => '||', }
  if binary.keys.include?(@function)
    first = true
    @arguments.each do |a|
      if first
        first = false
      else
        q.text " #{binary[@function]} "
      end
      q.pp a
    end
  else
    first = true
    q.pp @function
    q.text "!("
    @arguments.each do |a|
      if first
        first = false
      else
        q.text ', '
      end
      q.pp a
    end
    q.text ")"
  end
end