Skip to content

Commit 74fc243

Browse files
committed
Add IO module
1 parent 5d2fda0 commit 74fc243

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

lib/elixir_script/translator/state.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ defmodule ElixirScript.Translator.State do
3838
|> Map.put(List, ElixirScript.List)
3939
|> Map.put(Process, ElixirScript.Process)
4040
|> Map.put(Regex, ElixirScript.Regex)
41+
|> Map.put(IO, ElixirScript.IO)
4142
end
4243

4344
def set_module_data(pid, module_data) do
@@ -135,7 +136,7 @@ defmodule ElixirScript.Translator.State do
135136

136137
def list_module_references(pid) do
137138
Agent.get(pid, fn(state) ->
138-
Enum.map(state.modules, fn {name, module} ->
139+
Enum.map(state.modules, fn {name, module} ->
139140
{name, Map.get(module, :refs, [])}
140141
end)
141142
end)
@@ -157,4 +158,3 @@ defmodule ElixirScript.Translator.State do
157158
Agent.stop(pid)
158159
end
159160
end
160-

priv/std_lib/io.ex

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
defmodule ElixirScript.IO do
2+
3+
def inspect(item, opts \\ []) do
4+
:console.log(item)
5+
item
6+
end
7+
8+
def puts(device \\ :stdio, item) when is_binary(item) do
9+
case device do
10+
:stdio ->
11+
:console.log(item)
12+
:stderr ->
13+
:console.warn(item)
14+
end
15+
end
16+
17+
def warn(message) when is_binary(message) do
18+
:console.warn("warning: #{message}")
19+
:console.trace()
20+
end
21+
22+
end

test/translator/try_test.exs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ defmodule ElixirScript.Translator.Try.Test do
1616
Bootstrap.Core.SpecialForms._try(function() {
1717
return 1;
1818
}, Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.type(ArgumentError, {})], function() {
19-
return IO.puts('Invalid argument given');
19+
return Elixir.ElixirScript.IO.__load(Elixir).puts('Invalid argument given');
2020
})), null, null, null)
2121
"""
2222

@@ -38,7 +38,7 @@ defmodule ElixirScript.Translator.Try.Test do
3838
Bootstrap.Core.SpecialForms._try(function() {
3939
return 1;
4040
}, Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.type(ArgumentError, {})], function() {
41-
return IO.puts('Invalid argument given');
41+
return Elixir.ElixirScript.IO.__load(Elixir).puts('Invalid argument given');
4242
})), null, null, null)
4343
"""
4444

@@ -60,7 +60,7 @@ defmodule ElixirScript.Translator.Try.Test do
6060
Bootstrap.Core.SpecialForms._try(function() {
6161
return 1;
6262
},Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.variable()],function(x) {
63-
return IO.puts('Invalid argument given');
63+
return Elixir.ElixirScript.IO.__load(Elixir).puts('Invalid argument given');
6464
},function(x) {
6565
return Bootstrap.Core.Functions.contains(x,Object.freeze([ArgumentError.create(Object.freeze({}))]));
6666
})),null,null,null)
@@ -84,7 +84,7 @@ defmodule ElixirScript.Translator.Try.Test do
8484
Bootstrap.Core.SpecialForms._try(function() {
8585
return 1;
8686
},Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.variable()],function(x) {
87-
return IO.puts('Invalid argument given');
87+
return Elixir.ElixirScript.IO.__load(Elixir).puts('Invalid argument given');
8888
})),null,null,null)
8989
"""
9090

@@ -109,9 +109,9 @@ defmodule ElixirScript.Translator.Try.Test do
109109
Bootstrap.Core.SpecialForms._try(function() {
110110
return 1;
111111
}, Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.type(ArgumentError, {})], function() {
112-
return IO.puts('ArgumentError');
112+
return Elixir.ElixirScript.IO.__load(Elixir).puts('ArgumentError');
113113
}), Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.variable()], function(x) {
114-
return IO.puts('x');
114+
return Elixir.ElixirScript.IO.__load(Elixir).puts('x');
115115
})), null, null, null)
116116
"""
117117

@@ -135,9 +135,9 @@ defmodule ElixirScript.Translator.Try.Test do
135135
Bootstrap.Core.SpecialForms._try(function() {
136136
return 1;
137137
}, Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.type(ArgumentError, {})], function() {
138-
return IO.puts('Invalid argument given');
138+
return Elixir.ElixirScript.IO.__load(Elixir).puts('Invalid argument given');
139139
})), null, null, function() {
140-
return IO.puts('This is printed regardless if it failed or succeed');
140+
return Elixir.ElixirScript.IO.__load(Elixir).puts('This is printed regardless if it failed or succeed');
141141
})
142142
"""
143143

@@ -158,7 +158,7 @@ defmodule ElixirScript.Translator.Try.Test do
158158
Bootstrap.Core.SpecialForms._try(function() {
159159
return 1;
160160
},null,null,null,function() {
161-
return IO.puts('This is printed regardless if it failed or succeed');
161+
return Elixir.ElixirScript.IO.__load(Elixir).puts('This is printed regardless if it failed or succeed');
162162
})
163163
"""
164164

@@ -210,9 +210,9 @@ defmodule ElixirScript.Translator.Try.Test do
210210
Bootstrap.Core.SpecialForms._try(function() {
211211
return 1;
212212
}, Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.type(ArgumentError, {})], function() {
213-
return IO.puts('Invalid argument given');
213+
return Elixir.ElixirScript.IO.__load(Elixir).puts('Invalid argument given');
214214
})), Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Symbol.for('throw'), Symbol.for('Error')], function() {
215-
return IO.puts('caught error');
215+
return Elixir.ElixirScript.IO.__load(Elixir).puts('caught error');
216216
})), null, null)
217217
"""
218218

0 commit comments

Comments
 (0)