Class: MilkTea::CompileTime::BlockContext
- Inherits:
-
Object
- Object
- MilkTea::CompileTime::BlockContext
- Defined in:
- lib/milk_tea/core/compile_time.rb
Instance Attribute Summary collapse
-
#checker ⇒ Object
readonly
Returns the value of attribute checker.
Instance Method Summary collapse
- #apply_compile_time_binary(operator, left, right) ⇒ Object
- #evaluate_assignment(assignment, scopes:) ⇒ Object
- #evaluate_block(statements, scopes: nil) ⇒ Object
- #evaluate_expression(expression, scopes:) ⇒ Object
- #evaluate_for(statement, scopes:) ⇒ Object
- #evaluate_if(statement, scopes:) ⇒ Object
- #evaluate_local_decl(decl, scopes:) ⇒ Object
- #evaluate_match(statement, scopes:) ⇒ Object
- #evaluate_statement(statement, scopes:) ⇒ Object
- #evaluate_while(statement, scopes:) ⇒ Object
-
#initialize(checker, initial_variables: nil) ⇒ BlockContext
constructor
A new instance of BlockContext.
- #resolve_compile_time_call(call_expr, scopes:) ⇒ Object
- #try_array_constructor_call(call_expr, scopes:) ⇒ Object
- #try_const_function_call(call_expr, scopes:) ⇒ Object
- #try_struct_constructor_call(call_expr, scopes:) ⇒ Object
- #zero_numeric?(value) ⇒ Boolean
Constructor Details
#initialize(checker, initial_variables: nil) ⇒ BlockContext
Returns a new instance of BlockContext.
243 244 245 246 |
# File 'lib/milk_tea/core/compile_time.rb', line 243 def initialize(checker, initial_variables: nil) @checker = checker @variables = initial_variables || {} end |
Instance Attribute Details
#checker ⇒ Object (readonly)
Returns the value of attribute checker.
241 242 243 |
# File 'lib/milk_tea/core/compile_time.rb', line 241 def checker @checker end |
Instance Method Details
#apply_compile_time_binary(operator, left, right) ⇒ Object
328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/milk_tea/core/compile_time.rb', line 328 def apply_compile_time_binary(operator, left, right) case operator when "+" then left.is_a?(Numeric) && right.is_a?(Numeric) ? left + right : nil when "-" then left.is_a?(Numeric) && right.is_a?(Numeric) ? left - right : nil when "*" then left.is_a?(Numeric) && right.is_a?(Numeric) ? left * right : nil when "/" then left.is_a?(Numeric) && right.is_a?(Numeric) && !zero_numeric?(right) ? left / right : nil when "%" then left.is_a?(Integer) && right.is_a?(Integer) && !right.zero? ? left % right : nil when "&" then left.is_a?(Integer) && right.is_a?(Integer) ? left & right : nil when "|" then left.is_a?(Integer) && right.is_a?(Integer) ? left | right : nil when "^" then left.is_a?(Integer) && right.is_a?(Integer) ? left ^ right : nil when "<<" then left.is_a?(Integer) && right.is_a?(Integer) ? left << right : nil when ">>" then left.is_a?(Integer) && right.is_a?(Integer) ? left >> right : nil end end |
#evaluate_assignment(assignment, scopes:) ⇒ Object
315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/milk_tea/core/compile_time.rb', line 315 def evaluate_assignment(assignment, scopes:) value = evaluate_expression(assignment.value, scopes:) case assignment.target when AST::Identifier if assignment.operator != "=" current = @variables[assignment.target.name] value = apply_compile_time_binary(assignment.operator.chomp("="), current, value) end @variables[assignment.target.name] = value end value end |
#evaluate_block(statements, scopes: nil) ⇒ Object
248 249 250 251 252 253 254 255 256 |
# File 'lib/milk_tea/core/compile_time.rb', line 248 def evaluate_block(statements, scopes: nil) result = nil statements.each do |statement| result = evaluate_statement(statement, scopes:) end result end |
#evaluate_expression(expression, scopes:) ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/milk_tea/core/compile_time.rb', line 287 def evaluate_expression(expression, scopes:) case expression when AST::Identifier return @variables[expression.name] if @variables.key?(expression.name) @checker.evaluate_compile_time_const_value(expression, scopes:) else CompileTime.evaluate( expression, resolve_identifier: ->(id_expr) { return @variables[id_expr.name] if @variables.key?(id_expr.name) @checker.evaluate_compile_time_const_value(id_expr, scopes:) }, resolve_member_access: ->(ma_expr) { @checker.evaluate_compile_time_const_value(ma_expr, scopes:) }, resolve_call: ->(call_expr) { resolve_compile_time_call(call_expr, scopes:) }, ) end end |
#evaluate_for(statement, scopes:) ⇒ Object
366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/milk_tea/core/compile_time.rb', line 366 def evaluate_for(statement, scopes:) iterable = evaluate_expression(statement.iterable, scopes:) return nil unless iterable.is_a?(Array) result = nil loop_var_name = statement.binding.name iterable.each do |element| @variables[loop_var_name] = element statement.body.each { |body_stmt| evaluate_statement(body_stmt, scopes:) } end result end |
#evaluate_if(statement, scopes:) ⇒ Object
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/milk_tea/core/compile_time.rb', line 381 def evaluate_if(statement, scopes:) statement.branches.each do |branch| condition = evaluate_expression(branch.condition, scopes:) if CompileTime.boolean_value?(condition) && condition branch.body.each { |body_stmt| evaluate_statement(body_stmt, scopes:) } return condition end end if statement.else_body statement.else_body.each { |body_stmt| evaluate_statement(body_stmt, scopes:) } end nil end |
#evaluate_local_decl(decl, scopes:) ⇒ Object
307 308 309 310 311 312 313 |
# File 'lib/milk_tea/core/compile_time.rb', line 307 def evaluate_local_decl(decl, scopes:) return nil unless decl.value value = evaluate_expression(decl.value, scopes:) @variables[decl.name] = value value end |
#evaluate_match(statement, scopes:) ⇒ Object
397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/milk_tea/core/compile_time.rb', line 397 def evaluate_match(statement, scopes:) scrutinee = evaluate_expression(statement.expression, scopes:) return nil unless scrutinee statement.arms.each do |arm| wildcard = arm.pattern.is_a?(AST::Identifier) && arm.pattern.name == "_" if wildcard || CompileTime.equality_result(scrutinee, evaluate_expression(arm.pattern, scopes:)) == true arm.body.each { |body_stmt| evaluate_statement(body_stmt, scopes:) } return scrutinee end end nil end |
#evaluate_statement(statement, scopes:) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/milk_tea/core/compile_time.rb', line 258 def evaluate_statement(statement, scopes:) case statement when AST::LocalDecl evaluate_local_decl(statement, scopes:) when AST::ReturnStmt value = statement.value ? evaluate_expression(statement.value, scopes:) : nil raise ReturnValue.new(value) when AST::WhileStmt evaluate_while(statement, scopes:) when AST::ForStmt evaluate_for(statement, scopes:) when AST::MatchStmt evaluate_match(statement, scopes:) when AST::Assignment evaluate_assignment(statement, scopes:) when AST::IfStmt evaluate_if(statement, scopes:) when AST::ExpressionStmt evaluate_expression(statement.expression, scopes:) when AST::PassStmt, AST::BreakStmt, AST::ContinueStmt # no-op at compile time when AST::EmitStmt # emitted declarations are collected during lowering nil else nil end end |
#evaluate_while(statement, scopes:) ⇒ Object
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/milk_tea/core/compile_time.rb', line 347 def evaluate_while(statement, scopes:) result = nil iterations = 0 max_iterations = 10_000 while iterations < max_iterations condition = evaluate_expression(statement.condition, scopes:) break unless condition break unless CompileTime.boolean_value?(condition) statement.body.each { |body_stmt| evaluate_statement(body_stmt, scopes:) } iterations += 1 end raise Error, "compile-time while loop exceeded iteration limit" if iterations >= max_iterations result end |
#resolve_compile_time_call(call_expr, scopes:) ⇒ Object
412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/milk_tea/core/compile_time.rb', line 412 def resolve_compile_time_call(call_expr, scopes:) result = try_const_function_call(call_expr, scopes:) return result if result result = try_struct_constructor_call(call_expr, scopes:) return result if result result = try_array_constructor_call(call_expr, scopes:) return result if result @checker.evaluate_compile_time_const_value(call_expr, scopes:) end |
#try_array_constructor_call(call_expr, scopes:) ⇒ Object
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/milk_tea/core/compile_time.rb', line 485 def try_array_constructor_call(call_expr, scopes:) return unless call_expr.callee.is_a?(AST::Specialization) return unless @checker.respond_to?(:resolve_type_expression) resolved = @checker.resolve_type_expression(call_expr.callee) return unless resolved && @checker.respond_to?(:array_type?) && @checker.array_type?(resolved) values = [] call_expr.arguments.each do |argument| val = evaluate_expression(argument.value, scopes:) return nil unless val values << val end values end |
#try_const_function_call(call_expr, scopes:) ⇒ Object
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/milk_tea/core/compile_time.rb', line 425 def try_const_function_call(call_expr, scopes:) return unless call_expr.callee.is_a?(AST::Identifier) func = @checker.top_level_function(call_expr.callee.name) return unless func&.ast&.respond_to?(:const) && func.ast.const begin initial_vars = {} func.ast.params.each_with_index do |param, idx| return nil if idx >= call_expr.arguments.length arg_expr = call_expr.arguments[idx].value arg_value = case arg_expr when AST::Identifier @variables[arg_expr.name] || @checker.evaluate_compile_time_const_value(arg_expr, scopes:) else CompileTime.evaluate( arg_expr, resolve_identifier: ->(id) { @variables[id.name] || @checker.evaluate_compile_time_const_value(id, scopes:) }, resolve_member_access: ->(ma) { @checker.evaluate_compile_time_const_value(ma, scopes:) }, resolve_type_ref: nil, resolve_call: ->(call_expr) { resolve_compile_time_call(call_expr, scopes:) }, ) end return nil unless arg_value initial_vars[param.name] = arg_value end ctx = BlockContext.new(@checker, initial_variables: initial_vars) ctx.evaluate_block(func.ast.body, scopes:) rescue ReturnValue => e e.value end end |
#try_struct_constructor_call(call_expr, scopes:) ⇒ Object
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
# File 'lib/milk_tea/core/compile_time.rb', line 460 def try_struct_constructor_call(call_expr, scopes:) types = if @checker.respond_to?(:types) @checker.types else @checker.instance_variable_get(:@ctx).types end callee_name = if call_expr.callee.is_a?(AST::Specialization) && call_expr.callee.callee.respond_to?(:name) call_expr.callee.callee.name elsif call_expr.callee.respond_to?(:name) call_expr.callee.name end return unless callee_name type = types[callee_name] return unless type.is_a?(Types::Struct) fields = {} call_expr.arguments.each do |argument| val = evaluate_expression(argument.value, scopes:) return nil unless val fields[argument.name] = val end fields end |
#zero_numeric?(value) ⇒ Boolean
343 344 345 |
# File 'lib/milk_tea/core/compile_time.rb', line 343 def zero_numeric?(value) (value.is_a?(Integer) && value.zero?) || (value.is_a?(Float) && value.zero?) end |