Skip to content

Commit

Permalink
Merge pull request #19 from daxida/bugfixing
Browse files Browse the repository at this point in the history
Add python templates, refactor codebase and fix bugs
  • Loading branch information
ellnix authored Mar 19, 2024
2 parents e04cfd4 + 810e7e5 commit 8ef1bb1
Show file tree
Hide file tree
Showing 25 changed files with 872 additions and 385 deletions.
4 changes: 2 additions & 2 deletions config/stub_templates/c/loopline.c.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ for (int i = 0; i < {{ count_var }}; i++) {
{%- else -%}
{% set len = "" %}
{%- endif %}
{{ type_kw }} {{ var.name }}{{ len }};
scanf("{{ sym }}", {{ var.name }});
{{ type_kw }} {{ var.ident }}{{ len }};
scanf("{{ sym }}", {{ var.ident }});
{%- endfor %}
}
11 changes: 6 additions & 5 deletions config/stub_templates/c/read_many.c.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
{% endif -%}
{% endif -%}

{% for cmt in comments -%}
// {{ cmt.variable }}: {{cmt.description}}
{%- for var in vars %}
{%- if var.input_comment -%}# {{ var.ident }}: {{ var.input_comment }}
{% endif -%}
{% endfor -%}

{# Setup of types (%d, %s etc.) and variable references (& or "") inside the scanf function #}
Expand All @@ -20,15 +21,15 @@
{%- else -%}
{%- set ref = "&" -%}
{%- endif -%}
{%- set var_ref = ref ~ var.name -%}
{%- set var_ref = ref ~ var.ident -%}
{%- set_global var_refs = var_refs | concat(with=var_ref) -%}
{%- set_global types = types ~ format_symbols[var.var_type] -%}
{%- endfor -%}

{%- if single_type -%}

{%- set_global type = vars[0].var_type -%}
{{ type_tokens[type] }} {{ vars | map(attribute="name") | join(sep=", ") }};
{{ type_tokens[type] }} {{ vars | map(attribute="ident") | join(sep=", ") }};
scanf("{{ types }}", {{ var_refs | join(sep=", ") }});

{%- else %}
Expand All @@ -39,7 +40,7 @@ scanf("{{ types }}", {{ var_refs | join(sep=", ") }});
{%- else -%}
{%- set len = "" -%}
{%- endif -%}
{{ type_tokens[var.var_type] }} {{ var.name }}{{ len }};
{{ type_tokens[var.var_type] }} {{ var.ident }}{{ len }};
{% endfor -%}
scanf("{{ types }}", {{ var_refs | join(sep=", ") }});

Expand Down
4 changes: 2 additions & 2 deletions config/stub_templates/c/read_one.c.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
{%- set ref = "&" -%}
{%- endif -%}

{{ type_kw }} {{ var.name }}{{ len }};
scanf("{{ sym }}", {{ ref }}{{ var.name }});
{{ type_kw }} {{ var.ident }}{{ len }};
scanf("{{ sym }}", {{ ref }}{{ var.ident }});
8 changes: 8 additions & 0 deletions config/stub_templates/python/loop.py.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{%- if debug_mode -%}
---- 🇱 OOP
{% endif -%}

for {{ index_ident }} in range({{ count_var }}):
{%- for line in inner %}
{{line}}
{%- endfor %}
48 changes: 48 additions & 0 deletions config/stub_templates/python/loopline.py.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{%- if debug_mode -%}
---- 🇱 OOPLINE
{%- if vars | length == 1 -%}
(SINGLE_TYPE)
{% else -%}
(MULTIPLE_TYPE)
{% endif -%}
{% endif -%}

{%- if vars | length == 1 -%}
{# SINGLE_TYPE #}
{%- set var = vars[0] -%}
{# Special CG case, doesn't make much sense otherwise #}
{%- if var.var_type == "Word" -%}
for {{ var.ident }} in input().split():
pass

{%- else -%}

for {{ index_ident }} in input().split():
{%- if var.var_type == "String" -%}
{%- set fn = index_ident -%}
{%- else -%}
{%- set fn = type_tokens[var.var_type] ~ "(" ~ index_ident ~ ")" -%}
{%- endif %}
{{ var.ident }} = {{ fn }}

{%- endif -%}
{# MULTIPLE_TYPE #}
{%- else -%}

inputs = input().split()
for {{ index_ident }} in range({{ count_var }}):
{%- for var in vars %}
{%- if loop.index0 == 0 -%}
{%- set idx = "" -%}
{%- else -%}
{%- set idx = "+" ~ loop.index0 -%}
{%- endif -%}
{%- if var.var_type == "String" or var.var_type == "Word" -%}
{%- set fn = "inputs[2*" ~ index_ident ~ idx ~ "]" -%}
{%- else -%}
{%- set fn = type_tokens[var.var_type] ~ "(inputs[2*" ~ index_ident ~ idx ~ "])" -%}
{%- endif %}
{{ var.ident }} = {{ fn }}
{%- endfor %}

{%- endif %}
13 changes: 13 additions & 0 deletions config/stub_templates/python/main.py.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% if debug_mode %}
---- 🇲 AIN
{% endif -%}
{% if debug_mode -%}
---- 🇸 TATEMENT
{% endif -%}

{%- for line in statement -%}
# {{ line }}
{% endfor %}
{%- for line in code_lines %}
{{ line }}
{%- endfor %}
39 changes: 39 additions & 0 deletions config/stub_templates/python/read_many.py.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{%- if debug_mode -%}
---- 🇷 EAD_MANY
{%- if single_type -%}
(SINGLE_TYPE)
{% else -%}
(MULTIPLE_TYPE)
{% endif -%}
{% endif -%}

{%- set_global names = vars | map(attribute="ident") | join(sep=", ") -%}

{%- if single_type -%}

{%- for var in vars %}
{%- if var.input_comment -%}# {{ var.ident }}: {{ var.input_comment }}
{% endif -%}
{% endfor -%}

{%- set_global type = vars[0].var_type -%}
{%- if type == "String" or type == "Word" -%}
{%- set assign = "input().split()" -%}
{%- else -%}
{%- set assign = "[" ~ type_tokens[type] ~ "(i) for i in input().split()]" -%}
{%- endif -%}
{{ names }} = {{ assign }}

{%- else -%}

inputs = input().split()
{%- for var in vars %}
{% if var.var_type == "String" or var.var_type == "Word" -%}
{%- set assign = "inputs[" ~ loop.index0 ~ "]" -%}
{% else -%}
{%- set assign = type_tokens[var.var_type] ~ "(inputs[" ~ loop.index0 ~ "])" -%}
{% endif -%}
{{ var.ident }} = {{ assign }}{%- if var.input_comment %} # {{ var.input_comment }}{% endif %}
{%- endfor -%}

{%- endif %}
13 changes: 13 additions & 0 deletions config/stub_templates/python/read_one.py.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% if debug_mode -%}
---- 🇷 EAD_ONE: {{ var.var_type }}
{% endif %}

{%- if var.var_type == "String" or var.var_type == "Word" -%}
{%- set fn = "input()" -%}
{%- elif var.var_type == "Bool" -%}
{%- set fn = "input()" ~ type_tokens[var.var_type] -%}
{%- else -%}
{%- set fn = type_tokens[var.var_type] ~ "(input())" -%}
{%- endif -%}

{{ var.ident }} = {{ fn }}{%- if var.input_comment %} # {{ var.input_comment }}{% endif %}
16 changes: 16 additions & 0 deletions config/stub_templates/python/stub_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name = "python"
variable_format = "snake_case"
source_file_ext = "py"
allow_uppercase_vars = false
keywords = [
"for"
]
aliases = ["py"]

[type_tokens]
Int = "int"
Long = "int"
Float = "float"
Bool = " != \"0\""
String = "UNUSED"
Word = "UNUSED"
10 changes: 10 additions & 0 deletions config/stub_templates/python/write.py.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{%- if debug_mode -%}
---- 🇼 RITE
{% endif -%}

{%- for line in output_comments -%}
# {{ line }}
{% endfor %}
{%- for line in messages -%}
print("{{line}}")
{% endfor -%}
20 changes: 20 additions & 0 deletions config/stub_templates/python/write_join.py.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{%- if debug_mode -%}
---- 🇼 RITE_JOIN
{% endif -%}

{%- set_global out = "" -%}
{%- for term in terms -%}
{%- if term.term_type == "Variable" -%}
{%- set_global out = out ~ "str(" ~ term.name ~ ")" -%}
{%- else -%}
{%- set_global out = out ~ '"' ~ term.name ~ '"' -%}
{%- endif -%}
{%- if loop.last == false %}
{%- set_global out = out ~ ' + " " + ' -%}
{% endif %}
{%- endfor -%}

{%- for line in output_comments %}
# {{ line }}
{%- endfor -%}
print({{ out | replace(from='" + "', to="") }})
9 changes: 5 additions & 4 deletions config/stub_templates/ruby/loopline.rb.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
---- 🇱 OOPLINE
{% endif -%}

{% for cmt in comments -%}
# {{ cmt.variable }}: {{cmt.description}}
{%- for var in vars %}
{%- if var.input_comment -%}# {{ var.ident }}: {{ var.input_comment }}
{% endif -%}
{% endfor -%}
gets.split.each{% if vars | length > 1 %}_slice({{ vars | length }}){% endif %} do |{{ vars | map(attribute="name") | join(sep=", ") }}|
gets.split.each{% if vars | length > 1 %}_slice({{ vars | length }}){% endif %} do |{{ vars | map(attribute="ident") | join(sep=", ") }}|
{%- for var in vars -%}
{%- if var.var_type != "Word" %}
{{var.name}} = {{var.name}}.{{type_tokens[var.var_type]-}}
{{var.ident}} = {{var.ident}}.{{type_tokens[var.var_type]-}}
{% endif -%}
{%- endfor %}
end
16 changes: 11 additions & 5 deletions config/stub_templates/ruby/read_many.rb.jinja
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
{%- if debug_mode -%}
---- 🇷 EAD_MANY
{%- if single_type -%}
(SINGLE_TYPE)
{% else -%}
(MULTIPLE_TYPE)
{% endif -%}
{% endif -%}

{% for cmt in comments -%}
# {{ cmt.variable }}: {{cmt.description}}
{%- for var in vars %}
{%- if var.input_comment -%}# {{ var.ident }}: {{ var.input_comment }}
{% endif -%}
{% endfor -%}

{{- vars | map(attribute="name") | join(sep=", ") }} = gets.split
{{- vars | map(attribute="ident") | join(sep=", ") }} = gets.split

{%- if single_type -%}

Expand All @@ -18,9 +24,9 @@

{%- for var in vars -%}
{%- if var.var_type == "String" or var.var_type == "Word" %}
{{var.name}} = {{var.name}}.chomp
{{var.ident}} = {{var.ident}}.chomp
{%- else %}
{{var.name}} = {{var.name}}.{{ type_tokens[var.var_type] }}
{{var.ident}} = {{var.ident}}.{{ type_tokens[var.var_type] }}
{%- endif -%}
{%- endfor -%}

Expand Down
2 changes: 1 addition & 1 deletion config/stub_templates/ruby/read_one.rb.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
{% set method = "." ~ type_tokens[var.var_type] %}
{%- endif -%}

{{ var.name }} = gets{{ method }}{% if comment %} # {{ comment.description }}{% endif %}
{{ var.ident }} = gets{{ method }}{% if var.input_comment %} # {{ var.input_comment }}{% endif %}
2 changes: 1 addition & 1 deletion config/stub_templates/ruby/write_join.rb.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

puts "
{%- for term in terms -%}
{%- if term.term_type=="Variable" -%}
{%- if term.term_type == "Variable" -%}
{{- '#{' }}{{ term.name }}{{ '}' -}}
{%- else -%}
{{- term.name -}}
Expand Down
4 changes: 2 additions & 2 deletions config/stub_templates/rust/loopline.rs.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# LOOPLINE
{% endif -%}

gets.split.each{% if vars | length > 1 %}_slice({{ vars | length }}){% endif %} do |{{ vars | map(attribute="name") | join(sep=", ") }}|
gets.split.each{% if vars | length > 1 %}_slice({{ vars | length }}){% endif %} do |{{ vars | map(attribute="ident") | join(sep=", ") }}|
{%- for var in vars -%}
{%- if var.var_type != "Word" %}
{{var.name}} = {{var.name}}.{{type_tokens[var.var_type]-}}
{{var.ident}} = {{var.ident}}.{{type_tokens[var.var_type]-}}
{% endif -%}
{%- endfor %}
end
13 changes: 7 additions & 6 deletions config/stub_templates/rust/read_many.rs.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
{% endif -%}
{% endif -%}

{% for cmt in comments -%}
// {{ cmt.variable }}: {{cmt.description}}
{%- for var in vars %}
{%- if var.input_comment -%}// {{ var.ident }}: {{ var.input_comment }}
{% endif -%}
{% endfor -%}

let mut input_line = String::new();
Expand All @@ -19,18 +20,18 @@ let inputs = input_line.split(" ").collect::<Vec<_>>();

{%- for var in vars -%}
{%- if var.var_type == "String" or var.var_type == "Word" %}
let {{var.name}} = inputs[{{ loop.index - 1 }}].trim().to_string();
let {{var.ident}} = inputs[{{ loop.index - 1 }}].trim().to_string();
{%- else %}
let {{var.name}} = parse_input!(inputs[{{ loop.index - 1 }}], {{ type_tokens[var.var_type] }});
let {{var.ident}} = parse_input!(inputs[{{ loop.index - 1 }}], {{ type_tokens[var.var_type] }});
{%- endif %}
{%- endfor -%}

{%- else %}
{%- for var in vars -%}
{%- if var.var_type == "String" or var.var_type == "Word" %}
let {{var.name}} = inputs[{{ loop.index - 1 }}].trim().to_string();
let {{var.ident}} = inputs[{{ loop.index - 1 }}].trim().to_string();
{%- else %}
let {{var.name}} = parse_input!(inputs[{{ loop.index - 1 }}], {{ type_tokens[var.var_type] }});
let {{var.ident}} = parse_input!(inputs[{{ loop.index - 1 }}], {{ type_tokens[var.var_type] }});
{%- endif -%}
{%- endfor -%}

Expand Down
4 changes: 2 additions & 2 deletions config/stub_templates/rust/read_one.rs.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();

{%- if var.var_type == "String" or var.var_type == "Word" %}
let {{ var.name }} = input_line.trim_matches('\n').to_string();
let {{ var.ident }} = input_line.trim_matches('\n').to_string();
{% else %}
let {{ var.name }} = parse_input!(input_line, {{ type_tokens[var.var_type] }});
let {{ var.ident }} = parse_input!(input_line, {{ type_tokens[var.var_type] }});
{% endif %}

{%- if comment -%}
Expand Down
Loading

0 comments on commit 8ef1bb1

Please sign in to comment.