Commit 256192f8 authored by HUANG Wei Chih's avatar HUANG Wei Chih
Browse files

add symbol table related API

parent 80654973
......@@ -450,6 +450,118 @@ else_statement( AST ) -->
:- devdoc( "\\section{Helper Functions}" ).
:- devdoc( "\\begin{itemize}" ).
% symbol table{{{
:- dynamic symbol/2.
add_symbol( Symbol, AttributeList ) :-
(
symbol( Symbol, _ )
->
true
;
assertz( symbol( Symbol, AttributeList ) )
).
remove_symbol( Symbol ) :-
retractall( symbol( Symbol, _ ) ).
replace_symbol( Symbol, AttributeList ) :-
remove_symbol( Symbol ),
add_symbol( Symbol, AttributeList ).
default_symbol_attr( Variable, AttributeList ) :-
\+ number( Variable ),
default_symbol_attr( Variable, no, no, 0, 0, 0, AttributeList ).
default_symbol_attr( Constant, AttributeList ) :-
number( Constant ),
default_symbol_attr( Constant, yes, no, Constant, Constant, Constant, AttributeList ).
default_symbol_attr( Symbol, Const, Dual, Init, Max, Min,
[ const: Const,
dual: Dual,
value: Symbol,
init: Init,
max: Max,
min: Min
] ).
get_symbol_name( Symbol ) :-
nonvar( Symbol ), !.
get_symbol_name( Symbol ) :-
var( Symbol ),
!,
constant( counter, CounterName ),
peek_count( CounterName, Counter ),
constant( tempPrefix, Prefix ),
atom_concat( Prefix, Counter, Symbol ),
count( CounterName, Counter ).
new_symbol( Symbol ) :-
new_symbol( Symbol, [] ).
new_symbol( Symbol, Init ) :-
number( Init ),
!,
new_symbol( Symbol, [ init: Init ] ).
new_symbol( Symbol, dual ) :-
!,
new_symbol( Symbol, dual, [] ).
new_symbol( Symbol, InitAttributeList ) :-
list( InitAttributeList ),
!,
get_symbol_name( Symbol ),
default_symbol_attr( Symbol, DefaultAttributeList ),
replace_attr( DefaultAttributeList, InitAttributeList, AttributeList ),
add_symbol( Symbol, AttributeList ).
new_symbol( Symbol, dual, Init ) :-
number( Init ),
!,
new_symbol( Symbol, dual, [ init: Init ] ).
new_symbol( Symbol, dual, InitAttributeList ) :-
list( InitAttributeList ),
!,
(
number( Symbol )
->
(
Symbol >= 0
->
P = Symbol, N = 0
;
SymbolN is -Symbol,
P = 0, N = SymbolN
)
;
get_symbol_name( Symbol ),
atom_concat( Symbol, '_p', P ),
atom_concat( Symbol, '_n', N )
),
append( InitAttributeList, [ dual: yes, value: [ P, N ] ], NewInitAttributeList ),
new_symbol( Symbol, NewInitAttributeList ).
replace_attr( AttributeList, [], AttributeList ).
replace_attr( AttributeList, [ Key: Value | ReplaceList ], ResultList ) :-
select( Key: _, AttributeList, Key: Value, AttributeListT ),
replace_attr( AttributeListT, ReplaceList, ResultList ).
get_symbol_attr( Symbol, QueryList ) :-
symbol( Symbol, AttributeList ),
instantiate_symbol_attr( AttributeList, QueryList ).
instantiate_symbol_attr( _, [] ).
instantiate_symbol_attr( AttributeList, [ Key: Value | QueryList ] ) :-
member( Key: Value, AttributeList ),
instantiate_symbol_attr( AttributeList, QueryList ).
% }}}
% init variable{{{
:- devdoc( "\\item init variable" ).
:- devdoc( "Initialize the variable with differential symantics." ).
......@@ -554,7 +666,8 @@ get_ode( OdeSystem, [ P, N ], DP, DN ) :-
Either using variable name to get the differential semantic, or using differential semantic to get the variable name.
Here are some functions to handle the variable database." ).
:- dynamic variable/2.
variable( VariableName, VariableDiff ) :-
get_symbol_attr( VariableName, [ value: VariableDiff ] ).
variable( [], [], list ).
......@@ -567,16 +680,7 @@ remove_variable( VariableName ) :-
type( VariableName, name ),
devdoc( "Remove variable with name \\emph{VariableName} from the database" ),
retractall( variable( VariableName, _ ) ).
add_variable( VariableName, VariableDiff ) :-
% add_variable( +VariableName, +VariableDiff )
type( VariableName, name ),
type( VariableDiff, "differential semantic" ),
devdoc( "Add variable with name \\emph{VariableName} and differential semantic \\emph{VariableDiff} to the database." ),
devdoc( "It is recommended to use add_variable( VariableName ) instead." ),
assertz( variable( VariableName, VariableDiff ) ).
remove_symbol( VariableName ).
add_variable( VariableName ) :-
% add_variable( +VariableName )
......@@ -591,15 +695,7 @@ add_variable( VariableName ) :-
;
throw( error( type_error( nonvar, VariableName ) ) )
),
(
variable( VariableName, _ )
->
true
;
atom_concat( VariableName, '_p', P ),
atom_concat( VariableName, '_n', N ),
add_variable( VariableName, [ P, N ] )
).
new_symbol( VariableName, dual ).
add_constant( Constant ) :-
% add_constant( +Constant:int )
......@@ -613,20 +709,7 @@ add_constant( Constant ) :-
;
throw( error( type_error( number, Constant ) ) )
),
(
variable( Constant, _ )
->
true
;
(
Constant >= 0
->
add_variable( Constant, [ Constant, 0 ] )
;
ConstantN is -Constant,
add_variable( Constant, [ 0, ConstantN ] )
)
).
new_symbol( Constant, dual ).
% }}}
% get_variable_name( ?Variable ){{{
:- devdoc( "\\item get variable name" ).
......@@ -636,17 +719,7 @@ get_variable_name( VariableName ) :-
type( VariableName, name ),
devdoc( "Generate temperory variable name if \\emph{VariableName} is not instantiate, do nothing otherwise." ),
nonvar( VariableName ), !.
get_variable_name( VariableName ) :-
% get_variable_name( --Variable )
var( VariableName ),
constant( counter, CounterName ),
peek_count( CounterName, Counter ),
constant( tempPrefix, TPrefix ),
atom_concat( TPrefix, Counter, VariableName ),
count( CounterName, Counter ).
get_symbol_name( VariableName ).
% }}}
% get_variable_positive( ?VariableName, -VariableP ){{{
:- devdoc( "\\item get variable positive" ).
......
Supports Markdown
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