Skip to content

Commit 5f4deb8

Browse files
committed
4.0.4
1 parent 387982c commit 5f4deb8

File tree

3 files changed

+235
-43
lines changed

3 files changed

+235
-43
lines changed

lib/conversions/src/RLCToRuby.cpp

Lines changed: 78 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ namespace mlir::rlc
3232
return (
3333
type and
3434
(mlir::isa<mlir::rlc::ClassType>(decayCtxFrmType(type)) or
35-
mlir::isa<mlir::rlc::AlternativeType>(decayCtxFrmType(type))));
35+
mlir::isa<mlir::rlc::AlternativeType>(decayCtxFrmType(type)) or
36+
mlir::isa<mlir::rlc::ArrayType>(decayCtxFrmType(type))));
3637
}
3738
static void printCFunDecl(
3839
StreamWriter& writer, mlir::FunctionType type, llvm::StringRef name)
@@ -210,11 +211,6 @@ namespace mlir::rlc
210211
static void printArg(
211212
mlir::Type type, llvm::StringRef name, StreamWriter& writer)
212213
{
213-
bool isUserDefined = false;
214-
if (type and (mlir::isa<mlir::rlc::ClassType>(decayCtxFrmType(type)) or
215-
mlir::isa<mlir::rlc::AlternativeType>(decayCtxFrmType(type))))
216-
isUserDefined = true;
217-
218214
bool isStringArg =
219215
mlir::isa_and_nonnull<mlir::rlc::StringLiteralType>(type);
220216
if (type.isa_and_nonnull<mlir::rlc::StringLiteralType>())
@@ -237,7 +233,7 @@ namespace mlir::rlc
237233
{
238234
writer.write(")");
239235
}
240-
if (isUserDefined)
236+
if (isUserDefined(type))
241237
writer.write(".__rlc_data");
242238
writer.write(" ");
243239
}
@@ -300,6 +296,7 @@ namespace mlir::rlc
300296
.Case([&](mlir::rlc::StringLiteralType t) {
301297
w.write("Fiddle::SIZEOF_VOIDP");
302298
})
299+
.Case([&](mlir::rlc::FloatType t) { w.write("Fiddle::SIZEOF_DOUBLE"); })
303300
.Case(
304301
[&](mlir::rlc::ClassType t) { w.write(t.mangledName(), ".size"); })
305302
.Case([&](mlir::rlc::AliasType t) {
@@ -327,12 +324,10 @@ namespace mlir::rlc
327324

328325
{
329326
auto _ = w.indent();
330-
bool isUserDefined = resultType.isa<mlir::rlc::ClassType>() or
331-
resultType.isa<mlir::rlc::AlternativeType>();
332327

333328
if (returnsVoid(type).failed())
334329
{
335-
if (isUserDefined)
330+
if (isUserDefined(resultType))
336331
{
337332
w.write("__to_return = ");
338333
w.writeType(resultType);
@@ -357,7 +352,7 @@ namespace mlir::rlc
357352
// extract to convert it to a python builtin type instead
358353
if (returnsVoid(type).failed())
359354
{
360-
if (isUserDefined)
355+
if (isUserDefined(resultType))
361356
{
362357
w.writenl("__to_return");
363358
}
@@ -578,32 +573,23 @@ namespace mlir::rlc
578573
}
579574

580575
static void emitMemberType(
581-
mlir::rlc::StreamWriter& w, mlir::Type type, llvm::StringRef name)
576+
mlir::rlc::StreamWriter& w,
577+
mlir::Type type,
578+
llvm::StringRef name,
579+
mlir::Type size = nullptr)
582580
{
583-
if (auto casted = type.dyn_cast<mlir::rlc::ClassType>())
581+
bool needs_rlc_introducer =
582+
isUserDefined(type) and not type.isa<mlir::rlc::ArrayType>();
583+
if (size != nullptr)
584584
{
585-
w.write("{", name, ": RLC_", casted.mangledName(), "}");
586-
return;
587-
}
588-
if (auto casted = type.dyn_cast<mlir::rlc::AlternativeType>())
589-
{
590-
w.write("{", name, ": ");
591-
w.write("RLC_");
592-
w.writeType(casted);
585+
w.write("{\"", name, "[");
586+
w.writeType(size);
587+
w.write("]\": ", (needs_rlc_introducer ? "RLC_" : ""));
588+
w.writeType(type, 1);
593589
w.write("}");
594590
return;
595591
}
596-
if (auto casted = type.dyn_cast<mlir::rlc::ArrayType>())
597-
{
598-
w.write("'");
599-
w.writeType(casted.getUnderlying(), 1);
600-
w.write(" ", name);
601-
w.write("[");
602-
w.write(casted.getSize(), "]");
603-
w.write("'");
604-
return;
605-
}
606-
w.write("{", name, ":");
592+
w.write("{", name, ":", (needs_rlc_introducer ? "RLC_" : ""));
607593
w.writeType(type, 1);
608594
w.write("}");
609595
}
@@ -812,6 +798,19 @@ namespace mlir::rlc
812798
}
813799
}
814800

801+
static void emitFiddleDeclaration(
802+
mlir::rlc::ArrayType type,
803+
mlir::rlc::StreamWriter& w,
804+
MemberFunctionsTable& table,
805+
mlir::rlc::ModuleBuilder& builder,
806+
mlir::rlc::EnumDeclarationOp enumDeclaration = nullptr)
807+
{
808+
w.writeType(type);
809+
w.writenl(" = struct([");
810+
emitMemberType(w, type.getUnderlying(), "content", type.getSize());
811+
w.writenl("])");
812+
}
813+
815814
static void emitFiddleDeclaration(
816815
mlir::rlc::AlternativeType type,
817816
mlir::rlc::StreamWriter& w,
@@ -874,6 +873,10 @@ namespace mlir::rlc
874873
{
875874
emitFiddleDeclaration(casted, w, table, builder);
876875
}
876+
if (auto casted = mlir::dyn_cast<mlir::rlc::ArrayType>(t))
877+
{
878+
emitFiddleDeclaration(casted, w, table, builder);
879+
}
877880
}
878881
}
879882

@@ -974,6 +977,43 @@ namespace mlir::rlc
974977
writer.writenl("end");
975978
}
976979

980+
static void emitDeclaration(
981+
mlir::rlc::ArrayType type,
982+
mlir::rlc::StreamWriter& w,
983+
MemberFunctionsTable& table,
984+
mlir::rlc::ModuleBuilder& builder)
985+
{
986+
w.write("class ");
987+
w.writeType(type);
988+
w.endLine();
989+
{
990+
auto _ = w.indent();
991+
w.writenl("def __rlc_data");
992+
{
993+
auto _ = w.indent();
994+
w.write("return @content");
995+
w.endLine();
996+
}
997+
w.writenl("end");
998+
emitSpecialFunctions(type, w, table, builder);
999+
1000+
w.writenl("def [](index)");
1001+
{
1002+
auto _2 = w.indent();
1003+
w.write("return ");
1004+
if (not isUserDefined(type.getUnderlying()))
1005+
w.writenl("@content[index].content");
1006+
else
1007+
{
1008+
w.writeType(type.getUnderlying());
1009+
w.writenl(".new(@content[index])");
1010+
}
1011+
}
1012+
w.writenl("end");
1013+
}
1014+
w.writenl("end");
1015+
}
1016+
9771017
static void emitDeclaration(
9781018
mlir::rlc::AlternativeType type,
9791019
mlir::rlc::StreamWriter& w,
@@ -1006,14 +1046,8 @@ namespace mlir::rlc
10061046
{
10071047
auto _ = w.indent();
10081048
w.write("return ");
1009-
bool isUserDefined = false;
1010-
if (type and (mlir::isa<mlir::rlc::ClassType>(
1011-
decayCtxFrmType(enumeration.value())) or
1012-
mlir::isa<mlir::rlc::AlternativeType>(
1013-
decayCtxFrmType(enumeration.value()))))
1014-
isUserDefined = true;
1015-
1016-
if (not isUserDefined)
1049+
1050+
if (not isUserDefined(enumeration.value()))
10171051
w.writenl(
10181052
"@content._data._alternative",
10191053
enumeration.index(),
@@ -1199,7 +1233,7 @@ namespace mlir::rlc
11991233
OS << "Float";
12001234
});
12011235
matcher.add([&](mlir::rlc::ArrayType type, llvm::raw_string_ostream& OS) {
1202-
OS << "Array";
1236+
OS << "RLC_" << typeToMangled(type);
12031237
});
12041238
matcher.add([&](mlir::rlc::ClassType type, llvm::raw_string_ostream& OS) {
12051239
OS << type.mangledName();
@@ -1241,7 +1275,7 @@ namespace mlir::rlc
12411275
OS << "MyLib::RLC_string_lit";
12421276
});
12431277
ser.add([&](mlir::rlc::ArrayType type, llvm::raw_string_ostream& OS) {
1244-
OS << "MyLib::RLC_array";
1278+
OS << "MyLib::RLC_" << typeToMangled(type);
12451279
});
12461280
ser.add([&](mlir::rlc::OwningPtrType type, llvm::raw_string_ostream& OS) {
12471281
OS << "MyLib::RLC_voidp";
@@ -1477,6 +1511,8 @@ namespace mlir::rlc
14771511
}
14781512
if (auto casted = mlir::dyn_cast<mlir::rlc::AlternativeType>(t))
14791513
emitDeclaration(casted, matcher.getWriter(), table, builder);
1514+
if (auto casted = mlir::dyn_cast<mlir::rlc::ArrayType>(t))
1515+
emitDeclaration(casted, matcher.getWriter(), table, builder);
14801516
}
14811517

14821518
//// emit declarations of free functions

python/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def copy_binaries(source_directory, destination_directory):
9999
target_bin_dir if os.name != "nt" else os.path.join("Lib", "site-packages")
100100
)
101101

102-
version="0.4.3"
102+
version="0.4.4"
103103
setup(
104104
name="rl_language_core",
105105
version=version,
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# REQUIRES: has_ruby
2+
# RUN: split-file %s %t
3+
4+
# RUN: rlc %t/source.rl -o %t/lib%sharedext -i %stdlib --shared
5+
# RUN: rlc %t/source.rl -o %t/lib%libext -i %stdlib --compile
6+
7+
# RUN: rlc %t/source.rl -o %t/library.rb -i %stdlib --ruby
8+
# RUN: ruby %t/to_run.rb
9+
10+
#--- source.rl
11+
import serialization.to_byte_vector
12+
import string
13+
import action
14+
15+
cls Board:
16+
BInt<0, 3>[9] slots
17+
Bool playerTurn
18+
19+
fun get(Int x, Int y) -> Int:
20+
return self.slots[x + (y * 3)].value
21+
22+
fun set(Int x, Int y, Int val):
23+
self.slots[x + (y * 3)].value = val
24+
25+
fun full() -> Bool:
26+
let x = 0
27+
28+
while x < 3:
29+
let y = 0
30+
while y < 3:
31+
if self.get(x, y) == 0:
32+
return false
33+
y = y + 1
34+
x = x + 1
35+
36+
return true
37+
38+
fun three_in_a_line_player_row(Int player_id, Int row) -> Bool:
39+
return self.get(0, row) == self.get(1, row) and self.get(0, row) == self.get(2, row) and self.get(0, row) == player_id
40+
41+
fun three_in_a_line_player(Int player_id) -> Bool:
42+
let x = 0
43+
while x < 3:
44+
if self.get(x, 0) == self.get(x, 1) and self.get(x, 0) == self.get(x, 2) and self.get(x, 0) == player_id:
45+
return true
46+
47+
if self.three_in_a_line_player_row(player_id, x):
48+
return true
49+
x = x + 1
50+
51+
if self.get(0, 0) == self.get(1, 1) and self.get(0, 0) == self.get(2, 2) and self.get(0, 0) == player_id:
52+
return true
53+
54+
if self.get(0, 2) == self.get(1, 1) and self.get(0, 2) == self.get(2, 0) and self.get(0, 2) == player_id:
55+
return true
56+
57+
return false
58+
59+
fun current_player() -> Int:
60+
return int(self.playerTurn) + 1
61+
62+
fun next_turn():
63+
self.playerTurn = !self.playerTurn
64+
65+
# tic tac toe implementation
66+
67+
@classes
68+
act play() -> Game:
69+
frm board : Board
70+
frm score = 10
71+
while !board.full():
72+
# sets the indicated board as beloning
73+
# to the current player
74+
act mark(BInt<0, 3> x, BInt<0, 3> y) { board.get(x.value, y.value) == 0 }
75+
76+
score = score - 1
77+
board.set(x.value, y.value, board.current_player())
78+
79+
if board.three_in_a_line_player(board.current_player()):
80+
return
81+
board.next_turn()
82+
83+
fun get_current_player(Game g) -> Int:
84+
return int(g.board.playerTurn)
85+
86+
fun score(Game g, Int player_id) -> Float:
87+
if !g.is_done():
88+
return 0.0
89+
if g.board.three_in_a_line_player(player_id + 1):
90+
return 1.0
91+
else if g.board.three_in_a_line_player(((player_id + 1) % 2) + 1):
92+
return -1.0
93+
94+
return 0.0
95+
96+
fun get_num_players() -> Int:
97+
return 2
98+
99+
fun fuzz(Vector<Byte> input):
100+
if input.size() == 0:
101+
return
102+
let state = play()
103+
let action : AnyGameAction
104+
parse_and_execute(state, action, input)
105+
106+
fun main() -> Int:
107+
let game = play()
108+
let x : BInt<0, 3>
109+
let y : BInt<0, 3>
110+
x.value = 0
111+
y.value = 0
112+
game.mark(x, y)
113+
if game.board.full():
114+
return 1
115+
x.value = 1
116+
y.value = 0
117+
game.mark(x, y)
118+
if game.board.full():
119+
return 2
120+
x.value = 1
121+
y.value = 1
122+
game.mark(x, y)
123+
if game.board.full():
124+
return 3
125+
x.value = 2
126+
y.value = 0
127+
game.mark(x, y)
128+
if game.board.full():
129+
return 4
130+
x.value = 2
131+
y.value = 2
132+
game.mark(x, y)
133+
if game.board.full():
134+
return 5
135+
if game.board.three_in_a_line_player(1):
136+
return 0
137+
else:
138+
return 1
139+
140+
fun pretty_print(Game g):
141+
let i = 0
142+
while i != 3:
143+
let to_print : String
144+
let y = 0
145+
while y != 3:
146+
to_print.append(to_string(g.board.get(i, y)))
147+
y = y + 1
148+
print(to_print)
149+
i = i + 1
150+
151+
fun ensure_template_creation(Game g):
152+
from_string(g, to_string(g))
153+
154+
#--- to_run.rb
155+
require_relative 'library'
156+
puts(RLC::to_ruby_str(RLC::to_string(RLC::play)))

0 commit comments

Comments
 (0)