Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
Rename ast2c and move c2ast to own compile unit
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreter committed Mar 17, 2018
1 parent aa7386f commit efc0331
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 98 deletions.
3 changes: 2 additions & 1 deletion Makefile.conf
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ SOURCES = \
sass2scss.cpp \
backtrace.cpp \
operators.cpp \
to_c.cpp \
ast2c.cpp \
c2ast.cpp \
to_value.cpp \
source_map.cpp \
subset_map.cpp \
Expand Down
29 changes: 13 additions & 16 deletions src/to_c.cpp → src/ast2c.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
#include "sass.hpp"
#include "to_c.hpp"
#include "ast2c.hpp"
#include "ast.hpp"

namespace Sass {

union Sass_Value* To_C::fallback_impl(AST_Node_Ptr n)
{ return sass_make_error("unknown type for C-API"); }

union Sass_Value* To_C::operator()(Boolean_Ptr b)
union Sass_Value* AST2C::operator()(Boolean_Ptr b)
{ return sass_make_boolean(b->value()); }

union Sass_Value* To_C::operator()(Number_Ptr n)
union Sass_Value* AST2C::operator()(Number_Ptr n)
{ return sass_make_number(n->value(), n->unit().c_str()); }

union Sass_Value* To_C::operator()(Custom_Warning_Ptr w)
union Sass_Value* AST2C::operator()(Custom_Warning_Ptr w)
{ return sass_make_warning(w->message().c_str()); }

union Sass_Value* To_C::operator()(Custom_Error_Ptr e)
union Sass_Value* AST2C::operator()(Custom_Error_Ptr e)
{ return sass_make_error(e->message().c_str()); }

union Sass_Value* To_C::operator()(Color_Ptr c)
union Sass_Value* AST2C::operator()(Color_Ptr c)
{ return sass_make_color(c->r(), c->g(), c->b(), c->a()); }

union Sass_Value* To_C::operator()(String_Constant_Ptr s)
union Sass_Value* AST2C::operator()(String_Constant_Ptr s)
{
if (s->quote_mark()) {
return sass_make_qstring(s->value().c_str());
Expand All @@ -31,10 +28,10 @@ namespace Sass {
}
}

union Sass_Value* To_C::operator()(String_Quoted_Ptr s)
union Sass_Value* AST2C::operator()(String_Quoted_Ptr s)
{ return sass_make_qstring(s->value().c_str()); }

union Sass_Value* To_C::operator()(List_Ptr l)
union Sass_Value* AST2C::operator()(List_Ptr l)
{
union Sass_Value* v = sass_make_list(l->length(), l->separator(), l->is_bracketed());
for (size_t i = 0, L = l->length(); i < L; ++i) {
Expand All @@ -43,7 +40,7 @@ namespace Sass {
return v;
}

union Sass_Value* To_C::operator()(Map_Ptr m)
union Sass_Value* AST2C::operator()(Map_Ptr m)
{
union Sass_Value* v = sass_make_map(m->length());
int i = 0;
Expand All @@ -55,7 +52,7 @@ namespace Sass {
return v;
}

union Sass_Value* To_C::operator()(Arguments_Ptr a)
union Sass_Value* AST2C::operator()(Arguments_Ptr a)
{
union Sass_Value* v = sass_make_list(a->length(), SASS_COMMA, false);
for (size_t i = 0, L = a->length(); i < L; ++i) {
Expand All @@ -64,11 +61,11 @@ namespace Sass {
return v;
}

union Sass_Value* To_C::operator()(Argument_Ptr a)
union Sass_Value* AST2C::operator()(Argument_Ptr a)
{ return a->value()->perform(this); }

// not strictly necessary because of the fallback
union Sass_Value* To_C::operator()(Null_Ptr n)
union Sass_Value* AST2C::operator()(Null_Ptr n)
{ return sass_make_null(); }

};
17 changes: 8 additions & 9 deletions src/to_c.hpp → src/ast2c.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
#ifndef SASS_TO_C_H
#define SASS_TO_C_H
#ifndef SASS_AST2C_H
#define SASS_AST2C_H

#include "ast_fwd_decl.hpp"
#include "operation.hpp"
#include "sass/values.h"

namespace Sass {

class To_C : public Operation_CRTP<union Sass_Value*, To_C> {
// override this to define a catch-all
union Sass_Value* fallback_impl(AST_Node_Ptr n);
class AST2C : public Operation_CRTP<union Sass_Value*, AST2C> {

public:

To_C() { }
~To_C() { }
AST2C() { }
~AST2C() { }

union Sass_Value* operator()(Boolean_Ptr);
union Sass_Value* operator()(Number_Ptr);
Expand All @@ -29,9 +27,10 @@ namespace Sass {
union Sass_Value* operator()(Arguments_Ptr);
union Sass_Value* operator()(Argument_Ptr);

// dispatch to fallback implementation
// return sass error if type is not supported
union Sass_Value* fallback(AST_Node_Ptr x)
{ return fallback_impl(x); }
{ return sass_make_error("unknown type for C-API"); }

};

}
Expand Down
64 changes: 64 additions & 0 deletions src/c2ast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "ast.hpp"
#include "units.hpp"
#include "position.hpp"
#include "backtrace.hpp"
#include "sass/values.h"
#include "ast_fwd_decl.hpp"
#include "error_handling.hpp"

namespace Sass {

Value_Ptr c2ast(union Sass_Value* v, Backtraces traces, ParserState pstate)
{
using std::strlen;
using std::strcpy;
Value_Ptr e = NULL;
switch (sass_value_get_tag(v)) {
case SASS_BOOLEAN: {
e = SASS_MEMORY_NEW(Boolean, pstate, !!sass_boolean_get_value(v));
} break;
case SASS_NUMBER: {
e = SASS_MEMORY_NEW(Number, pstate, sass_number_get_value(v), sass_number_get_unit(v));
} break;
case SASS_COLOR: {
e = SASS_MEMORY_NEW(Color, pstate, sass_color_get_r(v), sass_color_get_g(v), sass_color_get_b(v), sass_color_get_a(v));
} break;
case SASS_STRING: {
if (sass_string_is_quoted(v))
e = SASS_MEMORY_NEW(String_Quoted, pstate, sass_string_get_value(v));
else {
e = SASS_MEMORY_NEW(String_Constant, pstate, sass_string_get_value(v));
}
} break;
case SASS_LIST: {
List_Ptr l = SASS_MEMORY_NEW(List, pstate, sass_list_get_length(v), sass_list_get_separator(v));
for (size_t i = 0, L = sass_list_get_length(v); i < L; ++i) {
l->append(c2ast(sass_list_get_value(v, i), traces, pstate));
}
l->is_bracketed(sass_list_get_is_bracketed(v));
e = l;
} break;
case SASS_MAP: {
Map_Ptr m = SASS_MEMORY_NEW(Map, pstate);
for (size_t i = 0, L = sass_map_get_length(v); i < L; ++i) {
*m << std::make_pair(
c2ast(sass_map_get_key(v, i), traces, pstate),
c2ast(sass_map_get_value(v, i), traces, pstate));
}
e = m;
} break;
case SASS_NULL: {
e = SASS_MEMORY_NEW(Null, pstate);
} break;
case SASS_ERROR: {
error("Error in C function: " + std::string(sass_error_get_message(v)), pstate, traces);
} break;
case SASS_WARNING: {
error("Warning in C function: " + std::string(sass_warning_get_message(v)), pstate, traces);
} break;
default: break;
}
return e;
}

}
14 changes: 14 additions & 0 deletions src/c2ast.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef SASS_C2AST_H
#define SASS_C2AST_H

#include "position.hpp"
#include "backtrace.hpp"
#include "ast_fwd_decl.hpp"

namespace Sass {

Value_Ptr c2ast(union Sass_Value* v, Backtraces traces, ParserState pstate);

}

#endif
76 changes: 11 additions & 65 deletions src/eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
#include "position.hpp"
#include "sass/values.h"
#include "to_value.hpp"
#include "to_c.hpp"
#include "ast2c.hpp"
#include "c2ast.hpp"
#include "context.hpp"
#include "backtrace.hpp"
#include "lexer.hpp"
Expand Down Expand Up @@ -333,9 +334,9 @@ namespace Sass {
Sass_Function_Entry c_function = def->c_function();
Sass_Function_Fn c_func = sass_function_get_function(c_function);

To_C to_c;
AST2C ast2c;
union Sass_Value* c_args = sass_make_list(1, SASS_COMMA, false);
sass_list_set_value(c_args, 0, message->perform(&to_c));
sass_list_set_value(c_args, 0, message->perform(&ast2c));
union Sass_Value* c_val = c_func(c_args, c_function, ctx.c_compiler);
ctx.c_options.output_style = outstyle;
ctx.callee_stack.pop_back();
Expand Down Expand Up @@ -381,9 +382,9 @@ namespace Sass {
Sass_Function_Entry c_function = def->c_function();
Sass_Function_Fn c_func = sass_function_get_function(c_function);

To_C to_c;
AST2C ast2c;
union Sass_Value* c_args = sass_make_list(1, SASS_COMMA, false);
sass_list_set_value(c_args, 0, message->perform(&to_c));
sass_list_set_value(c_args, 0, message->perform(&ast2c));
union Sass_Value* c_val = c_func(c_args, c_function, ctx.c_compiler);
ctx.c_options.output_style = outstyle;
ctx.callee_stack.pop_back();
Expand Down Expand Up @@ -425,9 +426,9 @@ namespace Sass {
Sass_Function_Entry c_function = def->c_function();
Sass_Function_Fn c_func = sass_function_get_function(c_function);

To_C to_c;
AST2C ast2c;
union Sass_Value* c_args = sass_make_list(1, SASS_COMMA, false);
sass_list_set_value(c_args, 0, message->perform(&to_c));
sass_list_set_value(c_args, 0, message->perform(&ast2c));
union Sass_Value* c_val = c_func(c_args, c_function, ctx.c_compiler);
ctx.c_options.output_style = outstyle;
ctx.callee_stack.pop_back();
Expand Down Expand Up @@ -1054,22 +1055,22 @@ namespace Sass {
{ env }
});

To_C to_c;
AST2C ast2c;
union Sass_Value* c_args = sass_make_list(params->length(), SASS_COMMA, false);
for(size_t i = 0; i < params->length(); i++) {
Parameter_Obj param = params->at(i);
std::string key = param->name();
AST_Node_Obj node = fn_env.get_local(key);
Expression_Obj arg = Cast<Expression>(node);
sass_list_set_value(c_args, i, arg->perform(&to_c));
sass_list_set_value(c_args, i, arg->perform(&ast2c));
}
union Sass_Value* c_val = c_func(c_args, c_function, ctx.c_compiler);
if (sass_value_get_tag(c_val) == SASS_ERROR) {
error("error in C function " + c->name() + ": " + sass_error_get_message(c_val), c->pstate(), traces);
} else if (sass_value_get_tag(c_val) == SASS_WARNING) {
error("warning in C function " + c->name() + ": " + sass_warning_get_message(c_val), c->pstate(), traces);
}
result = cval_to_astnode(c_val, traces, c->pstate());
result = c2ast(c_val, traces, c->pstate());

ctx.callee_stack.pop_back();
traces.pop_back();
Expand Down Expand Up @@ -1467,61 +1468,6 @@ namespace Sass {
return 0;
}

// All the binary helpers.

Expression_Ptr cval_to_astnode(union Sass_Value* v, Backtraces traces, ParserState pstate)
{
using std::strlen;
using std::strcpy;
Expression_Ptr e = NULL;
switch (sass_value_get_tag(v)) {
case SASS_BOOLEAN: {
e = SASS_MEMORY_NEW(Boolean, pstate, !!sass_boolean_get_value(v));
} break;
case SASS_NUMBER: {
e = SASS_MEMORY_NEW(Number, pstate, sass_number_get_value(v), sass_number_get_unit(v));
} break;
case SASS_COLOR: {
e = SASS_MEMORY_NEW(Color, pstate, sass_color_get_r(v), sass_color_get_g(v), sass_color_get_b(v), sass_color_get_a(v));
} break;
case SASS_STRING: {
if (sass_string_is_quoted(v))
e = SASS_MEMORY_NEW(String_Quoted, pstate, sass_string_get_value(v));
else {
e = SASS_MEMORY_NEW(String_Constant, pstate, sass_string_get_value(v));
}
} break;
case SASS_LIST: {
List_Ptr l = SASS_MEMORY_NEW(List, pstate, sass_list_get_length(v), sass_list_get_separator(v));
for (size_t i = 0, L = sass_list_get_length(v); i < L; ++i) {
l->append(cval_to_astnode(sass_list_get_value(v, i), traces, pstate));
}
l->is_bracketed(sass_list_get_is_bracketed(v));
e = l;
} break;
case SASS_MAP: {
Map_Ptr m = SASS_MEMORY_NEW(Map, pstate);
for (size_t i = 0, L = sass_map_get_length(v); i < L; ++i) {
*m << std::make_pair(
cval_to_astnode(sass_map_get_key(v, i), traces, pstate),
cval_to_astnode(sass_map_get_value(v, i), traces, pstate));
}
e = m;
} break;
case SASS_NULL: {
e = SASS_MEMORY_NEW(Null, pstate);
} break;
case SASS_ERROR: {
error("Error in C function: " + std::string(sass_error_get_message(v)), pstate, traces);
} break;
case SASS_WARNING: {
error("Warning in C function: " + std::string(sass_warning_get_message(v)), pstate, traces);
} break;
default: break;
}
return e;
}

Selector_List_Ptr Eval::operator()(Selector_List_Ptr s)
{
SelectorStack rv;
Expand Down
2 changes: 0 additions & 2 deletions src/eval.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ namespace Sass {

};

Expression_Ptr cval_to_astnode(union Sass_Value* v, Backtraces traces, ParserState pstate = ParserState("[AST]"));

}

#endif
3 changes: 2 additions & 1 deletion src/fn_colors.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <cctype>
#include <iomanip>
#include "ast.hpp"
#include "fn_utils.hpp"
Expand Down Expand Up @@ -738,4 +739,4 @@ namespace Sass {

}

}
}
3 changes: 3 additions & 0 deletions src/fn_strings.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include <cctype>
#include "utf8.h"
#include "ast.hpp"
#include "sass.hpp"
#include "fn_utils.hpp"
#include "fn_strings.hpp"

Expand Down
6 changes: 4 additions & 2 deletions win/libsass.targets
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\sass_values.hpp" />
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\source_map.hpp" />
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\subset_map.hpp" />
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\to_c.hpp" />
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\ast2c.hpp" />
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\c2ast.hpp" />
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\to_value.hpp" />
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\units.hpp" />
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\utf8.h" />
Expand Down Expand Up @@ -124,7 +125,8 @@
<ClCompile Include="$(LIBSASS_SRC_DIR)\sass2scss.cpp" />
<ClCompile Include="$(LIBSASS_SRC_DIR)\source_map.cpp" />
<ClCompile Include="$(LIBSASS_SRC_DIR)\subset_map.cpp" />
<ClCompile Include="$(LIBSASS_SRC_DIR)\to_c.cpp" />
<ClCompile Include="$(LIBSASS_SRC_DIR)\ast2c.cpp" />
<ClCompile Include="$(LIBSASS_SRC_DIR)\c2ast.cpp" />
<ClCompile Include="$(LIBSASS_SRC_DIR)\to_value.cpp" />
<ClCompile Include="$(LIBSASS_SRC_DIR)\units.cpp" />
<ClCompile Include="$(LIBSASS_SRC_DIR)\utf8_string.cpp" />
Expand Down
Loading

0 comments on commit efc0331

Please sign in to comment.