Browse Source

Cleaned some concept syntax back to templates

master
Ludovic 'Archivist' Lagouardette 1 year ago
parent
commit
1e0cae17df
7 changed files with 54 additions and 38 deletions
  1. +14
    -5
      include/molasses/generator_primitives.h
  2. +2
    -0
      include/molasses/parser_primitives.h
  3. +28
    -26
      src/molasses/generator_primitives_x86_64_linux.cpp
  4. +1
    -1
      src/molasses/lexer.cpp
  5. +3
    -2
      tests/001.exp
  6. +3
    -2
      tests/002.exp
  7. +3
    -2
      tests/003.exp

+ 14
- 5
include/molasses/generator_primitives.h View File

@ -2,21 +2,30 @@
#include "molasses/parser_primitives.h" #include "molasses/parser_primitives.h"
namespace molasses { namespace molasses {
template<architecture_t Arch = architecture>
std::vector<std::string> initialize_stack(); std::vector<std::string> initialize_stack();
template<architecture_t Arch = architecture>
std::vector<std::string> generate_return(); std::vector<std::string> generate_return();
template<architecture_t Arch = architecture>
std::vector<std::string> generate_label(const std::string& target); std::vector<std::string> generate_label(const std::string& target);
template<architecture_t Arch = architecture>
std::vector<std::string> generate_push_int32(int32_t target); std::vector<std::string> generate_push_int32(int32_t target);
template<architecture_t Arch = architecture>
std::vector<std::string> generate_push_int64(int64_t target); std::vector<std::string> generate_push_int64(int64_t target);
template<architecture_t Arch = architecture>
std::vector<std::string> generate_call(const std::string& target); std::vector<std::string> generate_call(const std::string& target);
template<architecture_t Arch = architecture>
std::vector<std::string> generate_enter(); std::vector<std::string> generate_enter();
template<architecture_t Arch = architecture>
std::vector<std::string> generate_push_string_ptr(const symbol&); std::vector<std::string> generate_push_string_ptr(const symbol&);
template<architecture_t Arch = architecture>
std::vector<std::string> generate_string(const symbol&, const std::string&); std::vector<std::string> generate_string(const symbol&, const std::string&);
} }

+ 2
- 0
include/molasses/parser_primitives.h View File

@ -211,6 +211,8 @@ namespace molasses {
std::vector<std::string> generate(const generate_context&); std::vector<std::string> generate(const generate_context&);
parser_context register_integers(parser_context); parser_context register_integers(parser_context);
template<architecture_t Arch = architecture>
parser_context register_i32_operations(parser_context); parser_context register_i32_operations(parser_context);
bool type_check(const parser_context&, const lexed_output&, const std::vector<symbol>&, std::vector<std::string> execution_input, const std::vector<std::string>& execution_output); bool type_check(const parser_context&, const lexed_output&, const std::vector<symbol>&, std::vector<std::string> execution_input, const std::vector<std::string>& execution_output);

+ 28
- 26
src/molasses/generator_primitives_x86_64_linux.cpp View File

@ -46,7 +46,8 @@ namespace molasses {
return builder.str(); return builder.str();
} }
parser_context register_integers(parser_context ctx) requires(architecture == architecture_t::x86_64_linux)
// TODO: move to a platform independent file
parser_context register_integers(parser_context ctx)
{ {
ctx.types.push_back(std::make_shared<primitive_type>("i8", 1)); ctx.types.push_back(std::make_shared<primitive_type>("i8", 1));
ctx.types.push_back(std::make_shared<primitive_type>("i16", 2)); ctx.types.push_back(std::make_shared<primitive_type>("i16", 2));
@ -60,8 +61,8 @@ namespace molasses {
return ctx; return ctx;
} }
n">parser_context register_i32_operations(parser_context ctx)
requires (architecture == architecture_t::x86_64_linux) {
k">template<>
parser_context register_i32_operations<architecture_t::x86_64_linux>(parser_context ctx) {
ctx.operations.emplace_back( ctx.operations.emplace_back(
std::make_shared<molasses::primitive_operation>( std::make_shared<molasses::primitive_operation>(
std::string{"+"}, std::string{"+"},
@ -261,51 +262,52 @@ namespace molasses {
return ctx; return ctx;
} }
n">std::vector<std::string> generate_call(const std::string& target)
requires (architecture == architecture_t::x86_64_linux) {
k">template<>
std::vector<std::string> generate_call<architecture_t::x86_64_linux>(const std::string& target) {
return { return {
" call "+marshal(target)+"\n", " call "+marshal(target)+"\n",
}; };
} }
n">std::vector<std::string> generate_string(const symbol& representation, const std::string& string_value)
requires (architecture == architecture_t::x86_64_linux) {
k">template<>
std::vector<std::string> generate_string<architecture_t::x86_64_linux>(const symbol& representation, const std::string& string_value) {
return { return {
"__EMITED_STRING_____"+std::to_string(representation.id)+"___:\n", "__EMITED_STRING_____"+std::to_string(representation.id)+"___:\n",
" .asciz \""+escape(string_value)+"\"\n", " .asciz \""+escape(string_value)+"\"\n",
}; };
} }
std::vector<std::string> generate_push_string_ptr(const symbol& representation) {
template<>
std::vector<std::string> generate_push_string_ptr<architecture_t::x86_64_linux>(const symbol& representation) {
return { return {
" pushq $__EMITED_STRING_____"+std::to_string(representation.id)+"___\n" " pushq $__EMITED_STRING_____"+std::to_string(representation.id)+"___\n"
}; };
} }
n">std::vector<std::string> generate_push_int32(int32_t target)
requires (architecture == architecture_t::x86_64_linux) {
k">template<>
std::vector<std::string> generate_push_int32<architecture_t::x86_64_linux>(int32_t target) {
return { return {
" pushq $" +std::to_string(target)+ "\n" " pushq $" +std::to_string(target)+ "\n"
}; };
} }
n">std::vector<std::string> generate_push_int64(int64_t target)
requires (architecture == architecture_t::x86_64_linux) {
k">template<>
std::vector<std::string> generate_push_int64<architecture_t::x86_64_linux>(int64_t target) {
return { return {
" pushq $" +std::to_string(target)+ "\n" " pushq $" +std::to_string(target)+ "\n"
}; };
} }
std::vector<std::string> generate_label(const std::string& target)
k">requires (architecture == architecture_t::x86_64_linux) {
template<>
n">std::vector<std::string> generate_label<architecture_t::x86_64_linux>(const std::string& target) {
return { return {
marshal(target)+":\n" marshal(target)+":\n"
}; };
} }
std::vector<std::string> generate_return()
k">requires (architecture == architecture_t::x86_64_linux) {
template<>
n">std::vector<std::string> generate_return<architecture_t::x86_64_linux>() {
return { return {
" // Return to caller\n", " // Return to caller\n",
" addq $-8, %r10\n", " addq $-8, %r10\n",
@ -313,18 +315,18 @@ namespace molasses {
" retq\n" " retq\n"
}; };
} }
std::vector<std::string> generate_enter()
k">requires (architecture == architecture_t::x86_64_linux) {
template<>
n">std::vector<std::string> generate_enter<architecture_t::x86_64_linux>() {
return { return {
" // Prepare the function stack\n", " // Prepare the function stack\n",
" popq (%r10)\n" " popq (%r10)\n"
" addq $8, %r10\n", " addq $8, %r10\n",
}; };
} }
std::vector<std::string> initialize_stack()
k">requires (architecture == architecture_t::x86_64_linux) {
template<>
n">std::vector<std::string> initialize_stack<architecture_t::x86_64_linux>() {
std::vector<std::string> operations = { std::vector<std::string> operations = {
"code:\n", "code:\n",
" .skip 1000000\n", " .skip 1000000\n",

+ 1
- 1
src/molasses/lexer.cpp View File

@ -60,7 +60,7 @@ namespace molasses {
continue; continue;
} }
if(character == '\"') { if(character == '\"') {
if(builder.view().empty() && state == state_machine_t::normal) {
if(builder.str().empty() && state == state_machine_t::normal) {
state = state_machine_t::string; state = state_machine_t::string;
continue; continue;
} else if (state == state_machine_t::string) { } else if (state == state_machine_t::string) {

+ 3
- 2
tests/001.exp View File

@ -1,6 +1,7 @@
#!/usr/bin/expect #!/usr/bin/expect
set SUGAR_EXECUTABLE $::env(SUGAR_EXECUTABLE) set SUGAR_EXECUTABLE $::env(SUGAR_EXECUTABLE)
set BUILD_NAME 001.
proc abort {reason} { proc abort {reason} {
puts "test failed $reason" puts "test failed $reason"
@ -20,7 +21,7 @@ if {$value != 0} {
} }
spawn -noecho $SUGAR_EXECUTABLE tests/001/exit-with-1.mol lex parse /tmp/sugar.generated generate assemble
spawn -noecho $SUGAR_EXECUTABLE tests/001/exit-with-1.mol lex parse "/tmp/sugar.generated.$BUILD_NAME" generate assemble
expect { expect {
error { abort "failed to compile" } error { abort "failed to compile" }
eof { abort "didn't run clang" } eof { abort "didn't run clang" }
@ -37,7 +38,7 @@ if {$value != 0} {
abort "compiler crashed" abort "compiler crashed"
} }
spawn -noecho /tmp/sugar.generated
spawn -noecho /tmp/sugar.generated.$BUILD_NAME
expect eof expect eof
lassign [wait] pid spawnid os_error_flag value lassign [wait] pid spawnid os_error_flag value
if {$value != 1} { if {$value != 1} {

+ 3
- 2
tests/002.exp View File

@ -1,6 +1,7 @@
#!/usr/bin/expect #!/usr/bin/expect
set SUGAR_EXECUTABLE $::env(SUGAR_EXECUTABLE) set SUGAR_EXECUTABLE $::env(SUGAR_EXECUTABLE)
set BUILD_NAME 002.
proc abort {reason} { proc abort {reason} {
puts "test failed $reason" puts "test failed $reason"
@ -20,7 +21,7 @@ if {$value != 0} {
} }
spawn -noecho $SUGAR_EXECUTABLE tests/002/exit-with-1.mol lex parse /tmp/sugar.generated generate assemble
spawn -noecho $SUGAR_EXECUTABLE tests/002/exit-with-1.mol lex parse /tmp/sugar.generated.$BUILD_NAME generate assemble
expect { expect {
error { abort "failed to compile" } error { abort "failed to compile" }
eof { abort "didn't run clang" } eof { abort "didn't run clang" }
@ -37,7 +38,7 @@ if {$value != 0} {
abort "compiler crashed" abort "compiler crashed"
} }
spawn -noecho /tmp/sugar.generated
spawn -noecho /tmp/sugar.generated.$BUILD_NAME
expect eof expect eof
lassign [wait] pid spawnid os_error_flag value lassign [wait] pid spawnid os_error_flag value
if {$value != 1} { if {$value != 1} {

+ 3
- 2
tests/003.exp View File

@ -1,6 +1,7 @@
#!/usr/bin/expect #!/usr/bin/expect
set SUGAR_EXECUTABLE $::env(SUGAR_EXECUTABLE) set SUGAR_EXECUTABLE $::env(SUGAR_EXECUTABLE)
set BUILD_NAME 003.
proc abort {reason} { proc abort {reason} {
puts "test failed $reason" puts "test failed $reason"
@ -25,7 +26,7 @@ if {$value != 0} {
} }
spawn -noecho $SUGAR_EXECUTABLE tests/003/exit-with-1.mol lex tests/003/library.mol lex merge parse /tmp/sugar.generated generate assemble
spawn -noecho $SUGAR_EXECUTABLE tests/003/exit-with-1.mol lex tests/003/library.mol lex merge parse /tmp/sugar.generated.$BUILD_NAME generate assemble
expect { expect {
error { abort "failed to compile" } error { abort "failed to compile" }
eof { abort "didn't run clang" } eof { abort "didn't run clang" }
@ -42,7 +43,7 @@ if {$value != 0} {
abort "compiler crashed" abort "compiler crashed"
} }
spawn -noecho /tmp/sugar.generated
spawn -noecho /tmp/sugar.generated.$BUILD_NAME
expect { expect {
error { abort "failed to compile" } error { abort "failed to compile" }
eof { abort "didn't output" } eof { abort "didn't output" }

Loading…
Cancel
Save