Skip to content

Commit

Permalink
Only inherit globals
Browse files Browse the repository at this point in the history
  • Loading branch information
ngallagher committed Oct 21, 2018
1 parent 2e5b914 commit e15c3a7
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.snapscript.compile;

import junit.framework.TestCase;

public class ScopeInheritanceTest extends ScriptTestCase {

private static final String SOURCE =
"for(i in 0..2){\n"+
" fun(i);\n"+
"}\n"+
"\n"+
"func fun(i){\n"+
" println(i);\n"+
"}\n";

public void testInheritance() throws Exception {
assertScriptCompiles(SOURCE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public boolean isConstant() {
return false;
}

public boolean isWild() {
public boolean isStatic() {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,17 @@ public boolean isModule() {
public boolean isPrivate() {
return ModifierType.isPrivate(modifiers);
}

@Override
public boolean isConstant() {
return ModifierType.isConstant(modifiers);
}


@Override
public boolean isStatic() {
return ModifierType.isStatic(modifiers);
}

@Override
public String toString() {
return String.valueOf(constraint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ public String formatAccessError(Type type, String name) {

return builder.toString();
}

public String formatReferenceError(String name) {
StringBuilder builder = new StringBuilder();

builder.append("Property '");
builder.append(name);
builder.append("' not found in scope");

return builder.toString();
}

public String formatReferenceError(Type type, String name) {
StringBuilder builder = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ public Result handleGenericError(Scope scope, String name, Type[] list) {
String message = formatter.formatGenericError(name, list);
throw builder.createInternalException(message);
}


public Result handleReferenceError(Scope scope, String name) {
String message = formatter.formatReferenceError(name);
throw builder.createInternalException(message);
}

public Result handleReferenceError(Scope scope, Type type, String name) {
String message = formatter.formatReferenceError(type, name);
throw builder.createInternalException(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Result failCompileConstruction(Scope scope, Type type) {
}

public Result failCompileReference(Scope scope, String name) {
return compile.handleAccessError(scope, name);
return compile.handleReferenceError(scope, name);
}

public Result failCompileAccess(Scope scope, Type type, String name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package org.snapscript.core.scope.index;

import static org.snapscript.core.constraint.Constraint.NONE;

import org.snapscript.core.constraint.Constraint;
import org.snapscript.core.variable.Value;

public abstract class Local extends Value {

public static Local getConstant(Object value, String name) {
return new LocalConstant(value, name, null);
return new LocalConstant(value, name, NONE);
}

public static Local getConstant(Object value, String name, Constraint type) {
return new LocalConstant(value, name, type);
}

public static Local getReference(Object value, String name) {
return new LocalReference(value, name, null);
return new LocalReference(value, name, NONE);
}

public static Local getReference(Object value, String name, Constraint type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ public class LocalScopeExtractor {

private final boolean reference;
private final boolean extension;
private final boolean globals;

public LocalScopeExtractor(boolean reference, boolean extension) {
this(reference, extension, false);
}

public LocalScopeExtractor(boolean reference, boolean extension, boolean globals) {
this.reference = reference;
this.extension = extension;
this.globals = globals;
}

public Scope extract(Scope scope) {
Expand All @@ -33,17 +39,20 @@ public Scope extract(Scope original, Scope outer) {

for(Local local : table){
String name = local.getName();
Value value = inner.getValue(name);

if(value == null) {
if(reference) {
inner.addValue(name, local); // enable modification of local
} else {
Object object = local.getValue();
Constraint constraint = local.getConstraint();
Value constant = Value.getConstant(object, constraint);

inner.addValue(name, constant); // local is a visible constant
Constraint constraint = local.getConstraint();

if(!globals || constraint.isStatic()) {
Value value = inner.getValue(name);

if (value == null) {
if (reference) {
inner.addValue(name, local); // enable modification of local
} else {
Object object = local.getValue();
Value constant = Value.getConstant(object, constraint);

inner.addValue(name, constant); // local is a visible constant
}
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions snap-parse/src/main/resources/grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,15 @@ module-function = <annotation-list><access-modifier-list><function-name>'('<para
module-statement = {<try-statement> | <synchronized-statement> | <conditional-statement> | <type-definition> | <assignment-statement> | <expression-statement>};
module-part = {<module-import> | <module-function> | <module-declaration> | <module-statement>};
module-definition = ?<annotation-list>'module'_<module-name>{'{}' | '{'*<module-part>'}'};


global = <assignment-variable>?(':'<constraint>?_)?('='<assignment-expression>);
global-statement = <declaration-modifier>_<global>*(','<global>)';';

script-import = <import-static> | <import> | <import-list>;
script-function = {'function'|'func'}_<function-name>'('<parameter-list>')'?(':'<constraint>)<group-statement>;
script-statement = { <try-statement> |
<synchronized-statement> |
<declaration-statement> |
<global-statement> |
<assert-statement> |
<debug-statement> |
<conditional-statement> |
Expand Down
36 changes: 36 additions & 0 deletions snap-tree/src/main/java/org/snapscript/tree/Global.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.snapscript.tree;

import static org.snapscript.core.ModifierType.*;

import org.snapscript.core.Evaluation;
import org.snapscript.core.constraint.Constraint;
import org.snapscript.core.scope.Scope;
import org.snapscript.core.variable.Value;
import org.snapscript.tree.literal.TextLiteral;

public class Global extends Declaration {

public Global(TextLiteral identifier) {
this(identifier, null, null);
}

public Global(TextLiteral identifier, Constraint constraint) {
this(identifier, constraint, null);
}

public Global(TextLiteral identifier, Evaluation value) {
this(identifier, null, value);
}

public Global(TextLiteral identifier, Constraint constraint, Evaluation value) {
super(identifier, constraint, value);
}

public Value compile(Scope scope, int modifiers) throws Exception {
return super.compile(scope, modifiers | STATIC.mask | PUBLIC.mask);
}

public Value declare(Scope scope, int modifiers) throws Exception {
return super.declare(scope, modifiers | STATIC.mask | PUBLIC.mask);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.snapscript.tree.compile;

import static org.snapscript.core.ModifierType.STATIC;

import java.util.List;

import org.snapscript.core.constraint.Constraint;
Expand All @@ -16,7 +18,11 @@ public class FunctionScopeCompiler extends ScopeCompiler {
protected final GenericList generics;

public FunctionScopeCompiler(GenericList generics) {
this.extractor = new LocalScopeExtractor(false, true);
this(generics, false);
}

public FunctionScopeCompiler(GenericList generics, boolean globals) {
this.extractor = new LocalScopeExtractor(false, true, globals);
this.generics = generics;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ScriptFunction(FunctionName identifier, ParameterList parameters, Stateme
public ScriptFunction(FunctionName identifier, ParameterList parameters, Constraint constraint, Statement body){
this.reference = new AtomicReference<FunctionBody>();
this.constraint = new DeclarationConstraint(constraint);
this.compiler = new FunctionScopeCompiler(identifier);
this.compiler = new FunctionScopeCompiler(identifier, true);
this.builder = new ScriptFunctionBuilder(body);
this.execution = new NoExecution(NORMAL);
this.identifier = identifier;
Expand Down
2 changes: 2 additions & 0 deletions snap-tree/src/main/resources/instruction.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ map-entry-list = org.snapscript.tree.construct.MapEntryList
map-entry = org.snapscript.tree.construct.MapEntry
map-key = org.snapscript.tree.construct.MapKey
collection-index = org.snapscript.tree.collection.CollectionIndex
global = org.snapscript.tree.Global
global-statement = org.snapscript.tree.DeclarationStatement
declaration = org.snapscript.tree.Declaration
declaration-modifier = org.snapscript.tree.Modifier
declaration-statement = org.snapscript.tree.DeclarationStatement
Expand Down

0 comments on commit e15c3a7

Please sign in to comment.