Commit 888f6930 authored by Brice Videau's avatar Brice Videau

Literal symbols and definitions and precedence are now defined by the library.

parent 874b12bf
......@@ -38,6 +38,21 @@ ccs_expression_associativity = (ccs_associativity_type * _sz_expr).in_dll(libcco
ccs_expression_symbols = [x.decode() if x else x for x in (ct.c_char_p * _sz_expr).in_dll(libcconfigspace, "ccs_expression_symbols")]
ccs_expression_arity = (ct.c_int * _sz_expr).in_dll(libcconfigspace, "ccs_expression_arity")
class ccs_terminal_type(CEnumeration):
_members_ = [
('TERM_NONE', 0),
'TERM_TRUE',
'TERM_FALSE',
'TERM_STRING',
'TERM_IDENTIFIER',
'TERM_INTEGER',
'TERM_FLOAT' ]
_sz_term = len(ccs_terminal_type._members_)
ccs_terminal_precedence = (ct.c_int * _sz_term).in_dll(libcconfigspace, "ccs_terminal_precedence")
ccs_terminal_regexp = [x.decode() if x else x for x in (ct.c_char_p * _sz_term).in_dll(libcconfigspace, "ccs_terminal_regexp")]
ccs_terminal_symbols = [x.decode() if x else x for x in (ct.c_char_p * _sz_term).in_dll(libcconfigspace, "ccs_terminal_symbols")]
ccs_create_binary_expression = _ccs_get_function("ccs_create_binary_expression", [ccs_expression_type, ccs_datum_fix, ccs_datum_fix, ct.POINTER(ccs_expression)])
ccs_create_unary_expression = _ccs_get_function("ccs_create_unary_expression", [ccs_expression_type, ccs_datum_fix, ct.POINTER(ccs_expression)])
ccs_create_expression = _ccs_get_function("ccs_create_expression", [ccs_expression_type, ct.c_size_t, ct.POINTER(ccs_datum), ct.POINTER(ccs_expression)])
......@@ -189,6 +204,10 @@ class Expression(Object):
class Literal(Expression):
none_symbol = ccs_terminal_symbols[ccs_terminal_type.TERM_NONE]
true_aymbol = ccs_terminal_symbols[ccs_terminal_type.TERM_TRUE]
false_symbol = ccs_terminal_symbols[ccs_terminal_type.TERM_FALSE]
def __init__(self, handle = None, retain = False, value = None):
if handle is None:
handle = ccs_expression()
......@@ -217,11 +236,11 @@ class Literal(Expression):
if isinstance(v, str):
return repr(v)
elif v is None:
return "none"
return Literal.none_symbol
elif v is True:
return "true"
return Literal.true_aymbol
elif v is False:
return "false"
return Literal.false_symbol
else:
return "{}".format(v)
......
from parglare import Parser, Grammar
from .expression import Expression, Literal, Variable, List, ccs_expression_type, ccs_associativity_type, ccs_expression_precedence, ccs_expression_associativity, ccs_expression_symbols, ccs_expression_arity
from .expression import Expression, Literal, Variable, List, ccs_expression_type, ccs_associativity_type, ccs_expression_precedence, ccs_expression_associativity, ccs_expression_symbols, ccs_expression_arity, ccs_terminal_type, ccs_terminal_precedence, ccs_terminal_regexp
_associativity_map = {
ccs_associativity_type.LEFT_TO_RIGHT: "left",
......@@ -21,22 +21,28 @@ list: '[' list_item ']'
list_item: list_item ',' value
| value;
value: none
| btrue
| bfalse
| true
| false
| string
| identifier
| integer
| float;
terminals
none: /none/ {prefer};
btrue: /true/ {prefer};
bfalse: /false/ {prefer};
identifier: /[a-zA-Z_][a-zA-Z_0-9]*/;
string: /"([^\0\t\n\r\f"\\]|\\[0tnrf"\\])+"|'([^\0\t\n\r\f'\\]|\\[0tnrf'\\])+'/;
integer: /-?[0-9]+/;
float: /-?[0-9]+([eE][+-]?[0-9]+|\.[0-9]+([eE][+-]?[0-9]+)?)/;
"""
none: /%s/ {%d};
true: /%s/ {%d};
false: /%s/ {%d};
identifier: /%s/ {%d};
string: /%s/ {%d};
integer: /%s/ {%d};
float: /%s/ {%d};
""" % (ccs_terminal_regexp[ccs_terminal_type.TERM_NONE], ccs_terminal_precedence[ccs_terminal_type.TERM_NONE],
ccs_terminal_regexp[ccs_terminal_type.TERM_TRUE], ccs_terminal_precedence[ccs_terminal_type.TERM_TRUE],
ccs_terminal_regexp[ccs_terminal_type.TERM_FALSE], ccs_terminal_precedence[ccs_terminal_type.TERM_FALSE],
ccs_terminal_regexp[ccs_terminal_type.TERM_IDENTIFIER], ccs_terminal_precedence[ccs_terminal_type.TERM_IDENTIFIER],
ccs_terminal_regexp[ccs_terminal_type.TERM_STRING], ccs_terminal_precedence[ccs_terminal_type.TERM_STRING],
ccs_terminal_regexp[ccs_terminal_type.TERM_INTEGER], ccs_terminal_precedence[ccs_terminal_type.TERM_INTEGER],
ccs_terminal_regexp[ccs_terminal_type.TERM_FLOAT], ccs_terminal_precedence[ccs_terminal_type.TERM_FLOAT])
_actions = {}
_expr_actions = [ lambda _, n: n[1] ]
......@@ -56,8 +62,8 @@ _actions["list_item"] = [
lambda _, n: [n[0]]
]
_actions["none"] = lambda _, value: Literal(value = None)
_actions["btrue"] = lambda _, value: Literal(value = True)
_actions["bfalse"] = lambda _, value: Literal(value = False)
_actions["true"] = lambda _, value: Literal(value = True)
_actions["false"] = lambda _, value: Literal(value = False)
_actions["identifier"] = lambda p, value: Variable(hyperparameter = p.extra.hyperparameter_by_name(value))
_actions["string"] = lambda _, value: Literal(value = eval(value))
_actions["float"] = lambda _, value: Literal(value = float(value))
......
......@@ -56,6 +56,30 @@ module CCS
[k, expression_arity[v]]
}.to_h
TerminalType = enum FFI::Type::INT32, :ccs_terminal_type_t, [
:CCS_TERM_NONE, 0,
:CCS_TERM_TRUE,
:CCS_TERM_FALSE,
:CCS_TERM_STRING,
:CCS_TERM_IDENTIFIER,
:CCS_TERM_INTEGER,
:CCS_TERM_FLOAT,
]
attach_variable :terminal_precedence, :ccs_terminal_precedence, FFI::ArrayType::new(find_type(:int), TerminalType.symbol_map.size)
attach_variable :terminal_regexp, :ccs_terminal_regexp, FFI::ArrayType::new(find_type(:string), TerminalType.symbol_map.size)
attach_variable :terminal_symbols, :ccs_terminal_symbols, FFI::ArrayType::new(find_type(:string), TerminalType.symbol_map.size)
TerminalPrecedence = TerminalType.symbol_map.collect { |k, v|
[k, terminal_precedence[v]]
}.to_h
TerminalRegexp = TerminalType.symbol_map.collect { |k, v|
[k, terminal_regexp[v]]
}.to_h
TerminalSymbols = TerminalType.symbol_map.collect { |k, v|
[k, terminal_symbols[v]]
}.to_h
attach_function :ccs_create_binary_expression, [:ccs_expression_type_t, :ccs_datum_t, :ccs_datum_t, :pointer], :ccs_result_t
attach_function :ccs_create_unary_expression, [:ccs_expression_type_t, :ccs_datum_t, :pointer], :ccs_result_t
attach_function :ccs_create_expression, [:ccs_expression_type_t, :size_t, :pointer, :pointer], :ccs_result_t
......@@ -186,6 +210,10 @@ module CCS
end
class Literal < Expression
NONE_SYMBOL = TerminalSymbols[:CCS_TERM_NONE]
TRUE_SYMBOL = TerminalSymbols[:CCS_TERM_TRUE]
FALSE_SYMBOL = TerminalSymbols[:CCS_TERM_FALSE]
def initialize(handle = nil, retain: false, value: nil)
if handle
super(handle, retain: retain)
......@@ -207,7 +235,11 @@ module CCS
def to_s
case value
when nil
"none"
NONE_SYMBOL
when true
TRUE_SYMBOL
when false
FALSE_SYMBOL
when String
value.inspect
else
......
......@@ -43,25 +43,25 @@ module CCS
rule("]")
rule(",")
rule(:none => /none/).as { |b|
rule(:none => Regexp.new(TerminalRegexp[:CCS_TERM_NONE])).as { |b|
Literal::new(value: nil) }
rule(:btrue => /true/).as { |b|
rule(:true => Regexp.new(TerminalRegexp[:CCS_TERM_TRUE])).as { |b|
Literal::new(value: true) }
rule(:bfalse => /false/).as { |b|
rule(:false => Regexp.new(TerminalRegexp[:CCS_TERM_FALSE])).as { |b|
Literal::new(value: false) }
rule(:float => /-?[0-9]+([eE][+-]?[0-9]+|\.[0-9]+([eE][+-]?[0-9]+)?)/).as {|num|
rule(:float => Regexp.new(TerminalRegexp[:CCS_TERM_FLOAT])).as {|num|
Literal::new(value: Float(num)) }
rule(:integer => /-?[0-9]+/).as { |num|
rule(:integer => Regexp.new(TerminalRegexp[:CCS_TERM_INTEGER])).as { |num|
Literal::new(value: Integer(num)) }
rule(:identifier => /[a-zA-Z_][a-zA-Z_0-9]*/).as { |identifier|
rule(:identifier => Regexp.new(TerminalRegexp[:CCS_TERM_IDENTIFIER])).as { |identifier|
Variable::new(hyperparameter: context.hyperparameter_by_name(identifier)) }
rule(:string => /"([^\0\t\n\r\f"\\]|\\[0tnrf"\\])+"|'([^\0\t\n\r\f'\\]|\\[0tnrf'\\])+'/).as { |str|
rule(:string => Regexp.new(TerminalRegexp[:CCS_TERM_STRING])).as { |str|
Literal::new(value: eval(str)) }
rule(:value) do |r|
r[:none]
r[:btrue]
r[:bfalse]
r[:true]
r[:false]
r[:string]
r[:identifier]
r[:float]
......
......@@ -29,7 +29,6 @@ enum ccs_expression_type_e {
CCS_EXPRESSION_TYPE_MAX,
CCS_EXPRESSION_FORCE_32BIT = INT_MAX
};
typedef enum ccs_expression_type_e ccs_expression_type_t;
// Precedence classes: least from most (taken from C)
......@@ -55,7 +54,6 @@ enum ccs_associativity_type_e {
CCS_ASSOCIATIVITY_TYPE_MAX,
CCS_ASSOCIATIVITY_TYPE_FORCE_32BIT = INT_MAX
};
typedef enum ccs_associativity_type_e ccs_associativity_type_t;
extern const ccs_associativity_type_t ccs_expression_associativity[];
......@@ -64,6 +62,29 @@ extern const char *ccs_expression_symbols[];
extern const int ccs_expression_arity[];
enum ccs_terminal_type_e {
CCS_TERM_NONE = 0,
CCS_TERM_TRUE,
CCS_TERM_FALSE,
CCS_TERM_STRING,
CCS_TERM_IDENTIFIER,
CCS_TERM_INTEGER,
CCS_TERM_FLOAT,
CCS_TERMINAL_TYPE_MAX,
CCS_TERMINAL_FORCE_32BIT = INT_MAX
};
typedef enum ccs_terminal_type_e ccs_terminal_type_t;
// Precedence classes for disambiguation
// NONE, TRUE and FALSE are not IDENTIFIER
// 0: STRING, IDENTIFIER, INTEGER, FLOAT
// 1: NONE, TRUE, FALSE
extern const int ccs_terminal_precedence[];
extern const char *ccs_terminal_regexp[];
extern const char *ccs_terminal_symbols[];
// Expressions
extern ccs_result_t
ccs_create_binary_expression(ccs_expression_type_t type,
......
......@@ -56,6 +56,31 @@ const int ccs_expression_arity[] = {
0, 0
};
const int ccs_terminal_precedence[] = {
1, 1, 1,
0, 0, 0, 0
};
const char *ccs_terminal_regexp[] = {
"none",
"true",
"false",
"\"([^\\0\\t\\n\\r\\f\"\\\\]|\\\\[0tnrf\"\\\\])+\"|'([^\\0\\t\\n\\r\\f'\\\\]|\\\\[0tnrf'\\\\])+'",
"[a-zA-Z_][a-zA-Z_0-9]*",
"-?[0-9]+",
"-?[0-9]+([eE][+-]?[0-9]+|\\.[0-9]+([eE][+-]?[0-9]+)?)"
};
const char *ccs_terminal_symbols[] = {
"none",
"true",
"false",
NULL,
NULL,
NULL,
NULL
};
static inline _ccs_expression_ops_t *
ccs_expression_get_ops(ccs_expression_t expression) {
return (_ccs_expression_ops_t *)expression->obj.ops;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment