forked from wavefnd/Wave
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathliterals.rs
More file actions
160 lines (133 loc) · 5.42 KB
/
literals.rs
File metadata and controls
160 lines (133 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use super::ExprGenEnv;
use inkwell::types::{BasicTypeEnum, StringRadix};
use inkwell::values::{BasicValue, BasicValueEnum};
use parser::ast::Literal;
fn parse_signed_decimal<'a>(s: &'a str) -> (bool, &'a str) {
if let Some(rest) = s.strip_prefix('-') {
(true, rest)
} else {
(false, s)
}
}
fn is_zero_decimal(s: &str) -> bool {
let s = s.trim();
let s = s.strip_prefix('+').unwrap_or(s);
let s = s.strip_prefix('-').unwrap_or(s);
!s.is_empty() && s.chars().all(|c| c == '0')
}
fn parse_int_radix(s: &str) -> (StringRadix, &str) {
if let Some(rest) = s.strip_prefix("0b").or_else(|| s.strip_prefix("0B")) {
(StringRadix::Binary, rest)
} else if let Some(rest) = s.strip_prefix("0x").or_else(|| s.strip_prefix("0X")) {
(StringRadix::Hexadecimal, rest)
} else if let Some(rest) = s.strip_prefix("0o").or_else(|| s.strip_prefix("0O")) {
(StringRadix::Octal, rest)
} else {
(StringRadix::Decimal, s)
}
}
pub(crate) fn gen<'ctx, 'a>(
env: &mut ExprGenEnv<'ctx, 'a>,
lit: &Literal,
expected_type: Option<BasicTypeEnum<'ctx>>,
) -> BasicValueEnum<'ctx> {
match lit {
Literal::Int(v) => match expected_type {
Some(BasicTypeEnum::IntType(int_ty)) => {
let s = v.as_str();
let (neg, raw) = parse_signed_decimal(s);
let (radix, digits) = parse_int_radix(raw);
let mut iv = int_ty
.const_int_from_string(digits, radix)
.unwrap_or_else(|| panic!("invalid int literal: {}", s));
if neg {
iv = iv.const_neg();
}
iv.as_basic_value_enum()
}
Some(BasicTypeEnum::ArrayType(at)) => {
let elem = at.get_element_type();
return gen(env, lit, Some(elem));
}
Some(BasicTypeEnum::PointerType(ptr_ty)) => {
let s = v.as_str();
let (neg, raw) = parse_signed_decimal(s);
let (radix, digits) = parse_int_radix(raw);
if neg {
panic!("negative pointer literal not allowed: {}", s);
}
let int_val = env
.context
.i64_type()
.const_int_from_string(digits, radix)
.unwrap_or_else(|| panic!("invalid pointer literal: {}", s));
env.builder
.build_int_to_ptr(int_val, ptr_ty, "int_to_ptr")
.unwrap()
.as_basic_value_enum()
}
Some(BasicTypeEnum::FloatType(ft)) => {
let f = v
.parse::<f64>()
.unwrap_or_else(|_| panic!("invalid float literal from int token: {}", v));
ft.const_float(f).as_basic_value_enum()
}
None => {
let s = v.as_str();
let (neg, raw) = parse_signed_decimal(s);
let (radix, digits) = parse_int_radix(raw);
let mut iv = env
.context
.i64_type()
.const_int_from_string(digits, radix)
.unwrap_or_else(|| panic!("invalid int literal: {}", s));
if neg {
iv = iv.const_neg();
}
iv.as_basic_value_enum()
},
_ => panic!("Unsupported expected_type for int literal: {:?}", expected_type),
}
Literal::Float(value) => match expected_type {
Some(BasicTypeEnum::FloatType(float_ty)) => float_ty.const_float(*value).as_basic_value_enum(),
Some(BasicTypeEnum::IntType(int_ty)) => env
.builder
.build_float_to_signed_int(
env.context.f32_type().const_float(*value),
int_ty,
"f32_to_int",
)
.unwrap()
.as_basic_value_enum(),
None => env.context.f32_type().const_float(*value).as_basic_value_enum(),
_ => panic!("Unsupported expected_type for float"),
},
Literal::String(value) => unsafe {
let bytes = value.as_bytes();
let mut null_terminated = bytes.to_vec();
null_terminated.push(0);
let global_name = format!("str_lit_{}", value.replace(" ", "_"));
let str_type = env
.context
.i8_type()
.array_type(null_terminated.len() as u32);
let global = env.module.add_global(str_type, None, &global_name);
global.set_initializer(&env.context.const_string(&null_terminated, false));
global.set_constant(true);
let zero = env.context.i32_type().const_zero();
let indices = [zero, zero];
let gep = env
.builder
.build_gep(global.as_pointer_value(), &indices, "str_gep")
.unwrap();
gep.as_basic_value_enum()
},
Literal::Bool(v) => env
.context
.bool_type()
.const_int(if *v { 1 } else { 0 }, false)
.as_basic_value_enum(),
Literal::Char(c) => env.context.i8_type().const_int(*c as u64, false).as_basic_value_enum(),
Literal::Byte(b) => env.context.i8_type().const_int(*b as u64, false).as_basic_value_enum(),
}
}