Compiler Errors :(

There Needs To Be a Super Advanced Button :)

Page 1 of 1

1 Replies - 889 Views - Last Post: 24 September 2009 - 08:34 AM Rate Topic: -----

#1 Asscotte  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 35
  • View blog
  • Posts: 610
  • Joined: 08-February 09

Compiler Errors :(

Post icon  Posted 24 September 2009 - 08:24 AM

Okay I Ported this code from a code project article in c# called Nscript

heres the code

Base App
Imports System
Imports System.IO
Imports System.Resources
Imports System.Reflection
Imports System.CodeDom.Compiler

Namespace NScript
	''' <summary>
	''' Summary description for BaseApp.
	''' </summary>
	Public MustInherit Class BaseApp
		Inherits MarshalByRefObject
		Implements IScriptManagerCallback
		Private Shared resMgr As New ResourceManager("NScript.NScript", GetType(BaseApp).Assembly)
		Private executionDomain As AppDomain
		Private m_fileName As String

		Public Sub New()
		End Sub

#Region "Overridables for derived classes"
		Protected Sub TerminateExecution()
			AppDomain.Unload(executionDomain)
		End Sub

		Protected MustOverride Sub ExecutionLoop(ByVal result As IAsyncResult)
		Protected MustOverride Sub TerminateExecutionLoop()
		Protected MustOverride Sub ShowErrorMessage(ByVal message As String)

#End Region

#Region "Utility function for derived classes"
		Protected ReadOnly Property FileName() As String
			Get
				Return Me.m_fileName
			End Get
		End Property

		Protected ReadOnly Property EntryAssemblyName() As String
			Get
				Return Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location)
			End Get
		End Property

		Protected Sub ShowErrorMessage(ByVal message As String, ByVal ParamArray args As Object())
			ShowErrorMessage([String].Format(message, args))
		End Sub

		Protected Sub ShowErrorMessageFromResource(ByVal id As String, ByVal ParamArray args As Object())
			ShowErrorMessage(resMgr.GetString(id), args)
		End Sub

		Protected Shared Function GetResourceString(ByVal name As String) As String
			Return resMgr.GetString(name)
		End Function

		Protected Shared Function GetResourceObject(ByVal name As String) As Object
			Return resMgr.GetObject(name)
		End Function
#End Region

#Region "Other Utility Methods"
		Private Sub ShowUsage()
			ShowErrorMessageFromResource("Usage", EntryAssemblyName)
		End Sub
#End Region

		Private Delegate Sub CompileAndExecuteRoutine(ByVal file As String, ByVal args As String(), ByVal callback As IScriptManagerCallback)

		Private Sub CompileAndExecute(ByVal file As String, ByVal args As String(), ByVal callback As IScriptManagerCallback)
			Try
				'Create an AppDomain to compile and execute the code
				'This enables us to cancel the execution if needed
				executionDomain = AppDomain.CreateDomain("ExecutionDomain")
				Dim manager As IScriptManager = DirectCast(executionDomain.CreateInstanceFromAndUnwrap(GetType(BaseApp).Assembly.Location, GetType(ScriptManager).FullName), IScriptManager)

				manager.CompileAndExecuteFile(file, args, Me)
			Catch e As UnsupportedLanguageExecption
				ShowErrorMessageFromResource("UnsupportedLanguage", e.Extension)
			Catch e As AppDomainUnloadedException
				System.Diagnostics.Trace.WriteLine(e.Message)
			Catch e As Exception
				ShowErrorMessage(e.Message)
			End Try

			TerminateExecutionLoop()
		End Sub

		Protected Sub Run(ByVal args As String())
			If args.Length < 1 Then
				ShowUsage()
				Exit Sub
			End If

			m_fileName = args(0)

			If Not File.Exists(m_fileName) Then
				ShowErrorMessageFromResource("FileDoesnotExist", args(0))
				Exit Sub
			End If

			'Create new argument array removing the file name
			Dim newargs As String() = New [String](args.Length - 2) {}
			Array.Copy(args, 1, newargs, 0, args.Length - 1)

			Dim asyncDelegate As New CompileAndExecuteRoutine(AddressOf CompileAndExecute)
			Dim result As IAsyncResult = asyncDelegate.BeginInvoke(m_fileName, newargs, Me, Nothing, Nothing)

			'For a windows app a message loop and for a console app a simple wait
			ExecutionLoop(result)

			asyncDelegate.EndInvoke(result)
		End Sub

#Region "Implementation of IScriptManagerCallback"
		Public Sub OnCompilerError(ByVal errors As CompilerError())
			Dim writer As New StringWriter()

			Dim errorFormat As String = BaseApp.GetResourceString("CompilerErrorFormat")

			For Each [error] As CompilerError In errors
				writer.WriteLine(errorFormat, [error].File, [error].Number, [error].Text, [error].Line, [error].Column)
			Next

			Throw New ApplicationException(writer.ToString())
		End Sub
#End Region
	End Class
End Namespace




Compiler Error
Imports System

Namespace NScript
	''' <summary>
	''' Summary description for CompilerError.
	''' </summary>
	<Serializable()> _
	Public Class CompilerError
		Private m_line As Integer
		Private m_file As String
		Private m_column As Integer
		Private m_text As String
		Private m_number As String

		Public Property Line() As Integer
			Get
				Return m_line
			End Get
			Set(ByVal value As Integer)
				m_line = value
			End Set
		End Property

		Public Property File() As String
			Get
				Return m_file
			End Get
			Set(ByVal value As String)
				m_file = value
			End Set
		End Property

		Public Property Column() As Integer
			Get
				Return m_column
			End Get
			Set(ByVal value As Integer)
				m_column = value
			End Set
		End Property

		Public Property Text() As String
			Get
				Return m_text
			End Get
			Set(ByVal value As String)
				m_text = value
			End Set
		End Property

		Public Property Number() As String
			Get
				Return m_number
			End Get
			Set(ByVal value As String)
				m_number = value
			End Set
		End Property

		Public Sub New()
		End Sub

		Public Sub New(ByVal [error] As System.CodeDom.Compiler.CompilerError)
			Me.m_column = [error].Column
			Me.m_file = [error].FileName
			Me.m_line = [error].Line
			Me.m_number = [error].ErrorNumber
			Me.m_text = [error].ErrorText
		End Sub
	End Class
End Namespace



IScriptManager
Imports System

Namespace NScript
	''' <summary>
	''' Summary description for IScriptManager.
	''' </summary>
	Public Interface IScriptManager
		Sub CompileAndExecuteFile(ByVal file As String, ByVal args As String(), ByVal callback As IScriptManagerCallback)
	End Interface
End Namespace





IScriptManagerCallback
Imports System

Namespace NScript
	''' <summary>
	''' Summary description for IScriptManagerCallback.
	''' </summary>
	Public Interface IScriptManagerCallback
		Sub OnCompilerError(ByVal errors As CompilerError())
	End Interface
End Namespace



Script Manager
Imports System
Imports System.IO
Imports System.CodeDom
Imports System.Configuration
Imports System.Reflection
Imports System.CodeDom.Compiler

Namespace NScript
	''' <summary>
	''' Summary description for ScriptManager.
	''' </summary>
	Public Class ScriptManager
		Inherits MarshalByRefObject
		Implements IScriptManager
		Public Sub New()
		End Sub

		Private Sub AddReferencesFromFile(ByVal compilerParams As CompilerParameters, ByVal nrfFile As String)
			Using reader As New StreamReader(nrfFile)
				Dim line As String



				While (InlineAssignHelper(line, reader.ReadLine())) IsNot Nothing
					compilerParams.ReferencedAssemblies.Add(line)
				End While
			End Using
		End Sub

#Region "Implementation of IScriptManager"
		Public Sub CompileAndExecuteFile(ByVal file__1 As String, ByVal args As String(), ByVal callback As IScriptManagerCallback)
			'Currently only csharp scripting is supported
			Dim provider As CodeDomProvider

			Dim extension As String = Path.GetExtension(file__1)

			Select Case extension
				Case ".cs", ".ncs"
					provider = New Microsoft.CSharp.CSharpCodeProvider()
					Exit Select
				Case ".vb", ".nvb"
					provider = New Microsoft.VisualBasic.VBCodeProvider()
					Exit Select
				Case ".njs", ".js"
					provider = DirectCast(Activator.CreateInstance("Microsoft.JScript", "Microsoft.JScript.JScriptCodeProvider").Unwrap(), CodeDomProvider)
					Exit Select
				Case Else
					Throw New UnsupportedLanguageExecption(extension)
			End Select

			Dim compiler As System.CodeDom.Compiler.ICodeCompiler = provider.CreateCompiler()

			Dim compilerparams As New System.CodeDom.Compiler.CompilerParameters()
			compilerparams.GenerateInMemory = True
			compilerparams.GenerateExecutable = True

			'Add assembly references from nscript.nrf or <file>.nrf
			Dim nrfFile As String = Path.ChangeExtension(file__1, "nrf")

			If File.Exists(nrfFile) Then
				AddReferencesFromFile(compilerparams, nrfFile)
			Else
				'Use nscript.nrf
				nrfFile = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "nscript.nrf")

				If File.Exists(nrfFile) Then
					AddReferencesFromFile(compilerparams, nrfFile)
				End If
			End If

			Dim results As System.CodeDom.Compiler.CompilerResults = compiler.CompileAssemblyFromFile(compilerparams, file__1)

			If results.Errors.HasErrors Then
				Dim templist As New System.Collections.ArrayList()

				For Each [error] As System.CodeDom.Compiler.CompilerError In results.Errors
					templist.Add(New CompilerError([error]))
				Next

				callback.OnCompilerError(DirectCast(templist.ToArray(GetType(CompilerError)), CompilerError()))
			Else
				results.CompiledAssembly.EntryPoint.Invoke(Nothing, BindingFlags.[Static], Nothing, New Object() {args}, Nothing)
			End If
		End Sub
		Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
			target = value
			Return value
		End Function
#End Region
	End Class
End Namespace



and finally;

Unsupported language exception
Imports System
Imports System.Runtime.Serialization

Namespace NScript
	''' <summary>
	''' Summary description for UnsupportedLanguageExecption.
	''' </summary>
	<Serializable()> _
	Public Class UnsupportedLanguageExecption
		Inherits Exception
		Implements ISerializable
		Private m_extension As String

		Public Sub New(ByVal info As SerializationInfo, ByVal ctxt As StreamingContext)
			MyBase.New(info, ctxt)
			m_extension = info.GetString("extension")
		End Sub

		Public Sub New(ByVal extension As String)
			Me.m_extension = extension
		End Sub

		Public Property Extension() As String
			Get
				Return m_extension
			End Get
			Set(ByVal value As String)
				m_extension = value
			End Set
		End Property

#Region "Implementation of ISerializable"
		Public Overloads Overrides Sub GetObjectData(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext)
			MyBase.GetObjectData(info, context)
			info.AddValue("extension", m_extension)
		End Sub
#End Region

	End Class
End Namespace



Okay Now I only get two errors (I dont know how to solve them)

Error	3	Class 'BaseApp' must implement 'Sub OnCompilerError(errors() As CompilerError)' for interface 'IScriptManagerCallback'.	



And

Error	4	Class 'ScriptManager' must implement 'Sub CompileAndExecuteFile(file As String, args() As String, callback As IScriptManagerCallback)' for interface 'IScriptManager'.	



But What I would also Like to know (as well as how to fix the errors), is would the code work?



Thanks for your time,

Is This A Good Question/Topic? 0
  • +

Replies To: Compiler Errors :(

#2 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Compiler Errors :(

Posted 24 September 2009 - 08:34 AM

You should do some reading about interfaces and how they work. In the mean time this should get you started.

In your BaseApp Class add this method:
 Public Sub OnCompilerError(ByVal errors() As CompilerError) Implements IScriptManagerCallback.OnCompilerError
End Sub



In your ScriptManager add this method:
   Public Sub CompileAndExecuteFile(ByVal file As String, ByVal args() As String, ByVal callback As IScriptManagerCallback) Implements IScriptManager.CompileAndExecuteFile

	End Sub

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1