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.
254 255 256 257 |
# File 'lib/milk_tea/core/compile_time.rb', line 254 def initialize(checker, initial_variables: nil) @checker = checker @variables = initial_variables || {} end |
Instance Attribute Details
#checker ⇒ Object (readonly)
Returns the value of attribute checker.
252 253 254 |
# File 'lib/milk_tea/core/compile_time.rb', line 252 def checker @checker end |
Instance Method Details
#apply_compile_time_binary(operator, left, right) ⇒ Object
343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/milk_tea/core/compile_time.rb', line 343 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
330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/milk_tea/core/compile_time.rb', line 330 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
259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/milk_tea/core/compile_time.rb', line 259 def evaluate_block(statements, scopes: nil) result = nil statements.each do |statement| outcome = evaluate_statement(statement, scopes:) return outcome if outcome.is_a?(ReturnOutcome) result = outcome end result end |
#evaluate_expression(expression, scopes:) ⇒ Object
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/milk_tea/core/compile_time.rb', line 302 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
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/milk_tea/core/compile_time.rb', line 384 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 do |body_stmt| outcome = evaluate_statement(body_stmt, scopes:) return outcome if outcome.is_a?(ReturnOutcome) end end result end |
#evaluate_if(statement, scopes:) ⇒ Object
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/milk_tea/core/compile_time.rb', line 402 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 do |body_stmt| outcome = evaluate_statement(body_stmt, scopes:) return outcome if outcome.is_a?(ReturnOutcome) end return condition end end if statement.else_body statement.else_body.each do |body_stmt| outcome = evaluate_statement(body_stmt, scopes:) return outcome if outcome.is_a?(ReturnOutcome) end end nil end |
#evaluate_local_decl(decl, scopes:) ⇒ Object
322 323 324 325 326 327 328 |
# File 'lib/milk_tea/core/compile_time.rb', line 322 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
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/milk_tea/core/compile_time.rb', line 424 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 do |body_stmt| outcome = evaluate_statement(body_stmt, scopes:) return outcome if outcome.is_a?(ReturnOutcome) end return scrutinee end end nil end |
#evaluate_statement(statement, scopes:) ⇒ Object
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/milk_tea/core/compile_time.rb', line 272 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 ReturnOutcome.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 nil when AST::EmitStmt # emitted declarations are collected during lowering nil else nil end end |
#evaluate_while(statement, scopes:) ⇒ Object
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/milk_tea/core/compile_time.rb', line 362 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 do |body_stmt| outcome = evaluate_statement(body_stmt, scopes:) return outcome if outcome.is_a?(ReturnOutcome) end 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
442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/milk_tea/core/compile_time.rb', line 442 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
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
# File 'lib/milk_tea/core/compile_time.rb', line 514 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
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 |
# File 'lib/milk_tea/core/compile_time.rb', line 455 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) result = ctx.evaluate_block(func.ast.body, scopes:) result.is_a?(ReturnOutcome) ? result.value : result end end |
#try_struct_constructor_call(call_expr, scopes:) ⇒ Object
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 |
# File 'lib/milk_tea/core/compile_time.rb', line 489 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
358 359 360 |
# File 'lib/milk_tea/core/compile_time.rb', line 358 def zero_numeric?(value) (value.is_a?(Integer) && value.zero?) || (value.is_a?(Float) && value.zero?) end |