diff options
author | David Robillard <d@drobilla.net> | 2010-12-31 20:23:24 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2010-12-31 20:23:24 +0000 |
commit | 4132dc03beb1c636a3d4b7df81700fe02a4cf70d (patch) | |
tree | 7cca1d81264f2370534b5411e8c4d1f73494998a | |
parent | 733e4d61c811dfec32953df51b7e57f853307a9b (diff) | |
download | resp-4132dc03beb1c636a3d4b7df81700fe02a4cf70d.tar.gz resp-4132dc03beb1c636a3d4b7df81700fe02a4cf70d.tar.bz2 resp-4132dc03beb1c636a3d4b7df81700fe02a4cf70d.zip |
Only compile symbols once (cache compiled symbol values specially for this).
Working pattern matching / deconstruction when object is a function parameter.
git-svn-id: http://svn.drobilla.net/resp/resp@396 ad02d1e2-f140-0410-9f75-f8b11f17cedd
-rw-r--r-- | src/compile.cpp | 9 | ||||
-rw-r--r-- | src/resp.hpp | 3 | ||||
-rw-r--r-- | test/match.resp | 14 |
3 files changed, 16 insertions, 10 deletions
diff --git a/src/compile.cpp b/src/compile.cpp index 53fd9ad..44017ec 100644 --- a/src/compile.cpp +++ b/src/compile.cpp @@ -39,15 +39,14 @@ compile_symbol(CEnv& cenv, const ASymbol* sym) throw() static CVal compile_literal_symbol(CEnv& cenv, const ASymbol* sym) throw() { - CVal* existing = cenv.vals.ref(sym); - if (existing) { - return *existing; + CEnv::CSyms::iterator i = cenv.cSyms.find(sym->sym()); + if (i != cenv.cSyms.end()) { + return i->second; } else { CVal compiled = cenv.engine()->compileString(cenv, sym->sym()); - cenv.vals.def(sym, compiled); + cenv.cSyms.insert(make_pair(sym->sym(), compiled)); return compiled; } - } static CVal diff --git a/src/resp.hpp b/src/resp.hpp index 4217f6d..7de26d8 100644 --- a/src/resp.hpp +++ b/src/resp.hpp @@ -777,6 +777,9 @@ struct CEnv { map<string,string> args; + typedef map<const char*, CVal> CSyms; + CSyms cSyms; + CFunc currentFn; ///< Currently compiling function bool repl; diff --git a/test/match.resp b/test/match.resp index 0d490ec..7f1c9dc 100644 --- a/test/match.resp +++ b/test/match.resp @@ -2,9 +2,13 @@ (Circle Float) (Rectangle Float Float)) -(def s1 (Circle 2.0)) -(def s2 (Rectangle 3.0 4.0)) +(def (area s) + (match s + (Rectangle w h) (* w h) + (Circle r) (* 3.14159 r))) -(match s2 - (Rectangle w h) (* w h) - (Circle r) (* 3.14159 r)) + +;(area (Circle 2.0)) + +(def s (Circle 2.0)) +(area s)
\ No newline at end of file |