1 # coding: utf-8
2 # frozen_string_literal: true
3
4
5
6 module Umu
7
8 module AbstractSyntax
9
10 module Core
11
12 module Expression
13
14 module Unary
15
16 class Raise < Abstract
17 alias exception_class obj
18 attr_reader :msg_expr
19
20
21 def initialize(loc, exception_class, msg_expr)
22 ASSERT.subclass_of exception_class, X::Abstraction::RuntimeError
23 ASSERT.kind_of msg_expr, ASCE::Abstract
24
25 super(loc, exception_class)
26
27 @msg_expr = msg_expr
28 end
29
30
31 def to_s
32 format("%%RAISE %s %s",
33 self.exception_class.to_s.split(/::/)[2],
34 self.msg_expr.to_s
35 )
36 end
37
38
39 def pretty_print(q)
40 q.text format("%%RAISE %s ",
41 self.exception_class.to_s.split(/::/)[2]
42 )
43 PRT.group q do
44 q.pp self.msg_expr
45 end
46 end
47
48
49 private
50
51 def __evaluate__(env, event)
52 new_env = env.enter event
53
54 msg_result = self.msg_expr.evaluate new_env
55 ASSERT.kind_of msg_result, ASR::Value
56
57 msg_value = msg_result.value
58 unless msg_value.kind_of? VCA::String
59 raise X::TypeError.new(
60 self.msg_expr.loc,
61 new_env,
62 "Expected a String, but %s : %s",
63 msg_value.to_s,
64 msg_value.type_sym.to_s
65 )
66 end
67
68 raise self.exception_class.new(
69 self.loc, env, msg_value.val.gsub(/%/, '%%')
70 )
71 end
72 end
73
74 end # Umu::AbstractSyntax::Core::Expression::Unary
75
76
77 module_function
78
79 def make_raise(loc, exception_class, msg_expr)
80 ASSERT.kind_of loc, LOC::Entry
81 ASSERT.subclass_of exception_class, X::Abstraction::RuntimeError
82 ASSERT.kind_of msg_expr, ASCE::Abstract
83
84 Unary::Raise.new(loc, exception_class, msg_expr).freeze
85 end
86
87 end # Umu::AbstractSyntax::Core::Expression
88
89 end # Umu::AbstractSyntax::Core
90
91 end # Umu::AbstractSyntax
92
93 end # Umu