package org.metaborg.meta.lang.dynsem.interpreter.nodes.matching;

import org.metaborg.meta.lang.dynsem.interpreter.utils.SourceSectionUtil;
import org.spoofax.interpreter.core.Tools;
import org.spoofax.interpreter.terms.IStrategoAppl;
import org.spoofax.terms.util.NotImplementedException;

import com.oracle.truffle.api.frame.FrameDescriptor;
import com.oracle.truffle.api.source.SourceSection;

public abstract class LiteralMatchPattern extends MatchPattern {

	public LiteralMatchPattern(SourceSection source) {
		super(source);
	}

	public static LiteralMatchPattern create(IStrategoAppl t, FrameDescriptor fd) {
		SourceSection source = SourceSectionUtil.fromStrategoTerm(t);
		if(Tools.hasConstructor(t, "True",0)){
			return new TrueLiteralTermMatchPattern(source);
		}
		if(Tools.hasConstructor(t, "False",0)){
			return new FalseLiteralTermMatchPattern(source);
		}
		if(Tools.hasConstructor(t, "Int", 1)){
			return new IntLiteralTermMatchPattern(Tools.intAt(t, 0).intValue(), source);
		}
		if(Tools.hasConstructor(t, "String", 1)){
			return new StringLiteralTermMatchPattern(Tools.stringAt(t, 0).stringValue(), source);
		}
		
		throw new NotImplementedException("Unsupported literal: " + t);
	}
}

@@ -675,24 +675,25 @@
 erm(t);%0A%09%09if
+ 
 (Tools.hasCo
@@ -712,19 +712,21 @@
  %22True%22,
+ 
 0))
+ 
 %7B%0A%09%09%09ret
@@ -770,32 +770,33 @@
 ource);%0A%09%09%7D%0A%09%09if
+ 
 (Tools.hasConstr
@@ -812,19 +812,21 @@
 %22False%22,
+ 
 0))
+ 
 %7B%0A%09%09%09ret
@@ -871,32 +871,33 @@
 ource);%0A%09%09%7D%0A%09%09if
+ 
 (Tools.hasConstr
@@ -911,24 +911,25 @@
 , %22Int%22, 1))
+ 
 %7B%0A%09%09%09return 
@@ -963,17 +963,37 @@
 ern(
+Integer.parseInt(
 Tools.
+str
 in
-t
+g
 At(t
@@ -1001,18 +1001,22 @@
  0).
+str
 in
-t
+g
 Value()
+)
 , so
@@ -1030,16 +1030,17 @@
 %09%09%7D%0A%09%09if
+ 
 (Tools.h
@@ -1069,16 +1069,17 @@
 ng%22, 1))
+ 
 %7B%0A%09%09%09ret
@@ -1165,18 +1165,16 @@
 e);%0A%09%09%7D%0A
-%09%09
 %0A%09%09throw

Fix literal pattern matching creation to correctly convert integer literals in the specification which are encoded as strings.